PRT is not a segment that is defined for the events you mentioned. Therefore you have to use a more generic way: You can add it to the message using addNonStandardSegment("PRT") (optionally with index position) and then obtain it with PRT prt = (PRT)a01.get("PRT")
This is because PR1 is not repeatable, but the surrounding PROCEDURE group is. String msg = "MSH|^~\\&|||||20200301114708.448+0530||ADT^A01^ADT_A01|106701|P|2.5\r" + "PR1|654^^^654~^^^654||^^^^^654^654|^^^^^^654\r" + "PR1|1\r" + "PR1|2\r" + "PR1|3"; Message a01 = new PipeParser().parse(msg); Terser terser = new Terser(a01); terser.set("/.PROCEDURE(1)/PR1-1-4", "234"); terser.set("/.PROCEDURE(2)/PR1-1-4", "345"); System.out.println(a01.toString().replace('\r', '\n')); and the output will be MSH|^~\&|||||20200301114708.448+0530||ADT^A01^ADT_A01|106701|P|2.5...
There is likely no proper HL7 segment separator after the 2.5.1 at the end of the MSH segment, so it treats the beginning of the next line as part of the version number. Segment separator must be a "\r" (corresponding to file saved with Mac linebreaks).
Hapi Terser issue to retrieve HL7 segments
Use version HAPI version 2.3 You need to specify the segments in the order they are defined in the message definition! - your example is completely mixed up Custom Z segments appended to a message can never be in a group - how should the parser know that ZAR and ZSH supposed to form a group? If a segment is not immediately repeating, the second occurence gets a custom segment name (with a number appended to the segment name) to make it unique. Here is a working example (should also work with 2.8.1):...
Please provide details about the MSH segment. The Terser paths depend on the event type and the HL7 version.
For RDE_O25, you use the RDE_O11 structure class
There is ca.uhn.hl7v2.preparser.PreParser.getFields(msg, fields...) that returns the value of fields without parsing it into a structure. Check out the javadocs