import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "PV1\\|(?:[^\\|]*\\|){18}1703000105\\|(?:[^\\|]*\\|){31}CO";
final String string = "MSH|^~\\&|SUNNY|SHSC|OACIS|SHSC|20210710161803||ORU^R01|SC20210710161803|P|2.3\n"
+ "PID|||7030389||Smith^John||20001010|M\n"
+ "PV1||I|||||||||||||||||1703000123|||||||||||||||||||||||||||||||\n"
+ "ORC|NW||||||||20210710161803\n"
+ "OBR|||SCVITALS-202107101618037402|SME^SunnyCare Manual Entry|||20210710010005|||~|||||||||||||||F\n"
+ "OBX|1|NM|0002-F828^SpRR||24|rpm|||||F|||20210710010005|||MANUAL\n"
+ "MSH|^~\\&|SUNNY|SHSC|OACIS|SHSC|20210710161803||ORU^R01|SC20210710161803|P|2.3\n"
+ "PID|||7030389||Smith^John||20001010|M\n"
+ "PV1||I|||||||||||||||||1703000105|||||||||||||||||||||||||||||||\n"
+ "ORC|CO||||||||20210710161803\n"
+ "OBR|||SCVITALS-202107101618037402|SME^SunnyCare Manual Entry|||20210710010005|||~|||||||||||||||F\n"
+ "OBX|1|NM|0002-F828^SpRR||24|rpm|||||F|||20210710010005|||MANUAL\n"
+ "MSH|^~\\&|SUNNY|SHSC|OACIS|SHSC|20210710161803||ORU^R01|SC20210710161803|P|2.3\n"
+ "PID|||7030389||Smith^John||20001010|M\n"
+ "PV1||I|||||||||||||||||1703000456|||||||||||||||||||||||||||||||\n"
+ "ORC|NW||||||||20210710161803\n"
+ "OBR|||SCVITALS-202107101618037402|SME^SunnyCare Manual Entry|||20210710010005|||~|||||||||||||||F\n"
+ "OBX|1|NM|0002-F828^SpRR||24|rpm|||||F|||20210710010005|||MANUAL\n"
+ "MSH|^~\\&|SUNNY|SHSC|OACIS|SHSC|20210710165010||ORU^R01|SC20210710165010|P|2.3\n"
+ "PID|||7030389||Smith^John||20001010|M\n"
+ "PV1||I|||||||||||||||||1703000105|||||||||||||||||||||||||||||||\n"
+ "ORC|NW||||||||20210710165010\n"
+ "OBR|||SCVITALS-202107101650108370|SME^SunnyCare Manual Entry|||20210710110005|||anpatel~anpatel|||||||||||||||F\n"
+ "OBX|1|NM|0002-F828^SpRR||34|rpm|||||F|||20210710110005|||MANUAL\n"
+ "MSH|^~\\&|SUNNY|SHSC|OACIS|SHSC|20210710165442||ORU^R01|SC20210710165442|P|2.3\n"
+ "PID|||7030389||Smith^John||20001010|M\n"
+ "PV1||I|||||||||||||||||1703000105|||||||||||||||||||||||||||||||\n"
+ "ORC|CO\n"
+ "OBR|||SCVITALS-202107101643353582|SME^SunnyCare Manual Entry|||20210710165442|||anpatel|||||||||||||||F\n"
+ "";
final Pattern pattern = Pattern.compile(regex);
final Matcher matcher = pattern.matcher(string);
while (matcher.find()) {
System.out.println("Full match: " + matcher.group(0));
for (int i = 1; i <= matcher.groupCount(); i++) {
System.out.println("Group " + i + ": " + matcher.group(i));
}
}
}
}
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for Java, please visit: https://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html