あなたString#split()
のニーズにとってはより簡単だと思います:
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);
}
出力:
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
を使いたい場合はPattern
、次の正規表現を使用できます[^|]+
。
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() + "\"");
}
出力 (両方でまったく同じ):
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
"