Reputation: 7482
I have this string which id like to delimit using Java Pattern. There is also a carriage return character after the first line. The delimiter character is |
MSH|^~\&|Unicare^HL7CISINV10.00.16^L||IBA||||ADT^A03|3203343722|P|2.3.1|||||
EVN|A03
I used the following code.
Pattern pattern = Pattern.compile("([^|]++)*");
Matcher matcher = pattern.matcher(str);
while (matcher.find()) {
System.out.println("Result: \"" + matcher.group() + "\"");
}
Doing this basically shows empty characters for each of the delimiter character. I would like find to ignore these. Any chance of modifying the regex so the characters can be ignored.
Thanks in advance.
Upvotes: 1
Views: 10416
Reputation: 135762
I believe String#split()
is simpler for your needs:
String src = "MSH|^~\\&|Unicare^HL7CISINV10.00.16^L||IBA||||ADT^A03|3203343722|P|2.3.1|||||\r\nEVN|A03\r";;
String[] ss = src.split("\\|+");
for (String s : ss) {
System.out.println(s);
}
Output:
MSH
^~\&
Unicare^HL7CISINV10.00.16^L
IBA
ADT^A03
3203343722
P
2.3.1
<--- there is a \r\n in the string at this point
EVN
A03
If you wanna go about using Pattern
, you can use the regex [^|]+
:
String str = "MSH|^~\\&|Unicare^HL7CISINV10.00.16^L||IBA||||ADT^A03|3203343722|P|2.3.1|||||\r\nEVN|A03\r";;
String[] ss = str.split("\\|+");
for (String s : ss) {
System.out.println("Split..: \"" + s + "\"");
}
Pattern pattern = Pattern.compile("[^|]+");
Matcher matcher = pattern.matcher(str);
while (matcher.find()) {
System.out.println("Pattern: \"" + matcher.group() + "\"");
}
Output (exactly the same for both):
Split..: "MSH"
Split..: "^~\&"
Split..: "Unicare^HL7CISINV10.00.16^L"
Split..: "IBA"
Split..: "ADT^A03"
Split..: "3203343722"
Split..: "P"
Split..: "2.3.1"
Split..: "
EVN"
Split..: "A03
"
Pattern: "MSH"
Pattern: "^~\&"
Pattern: "Unicare^HL7CISINV10.00.16^L"
Pattern: "IBA"
Pattern: "ADT^A03"
Pattern: "3203343722"
Pattern: "P"
Pattern: "2.3.1"
Pattern: "
EVN"
Pattern: "A03
"
Upvotes: 5