我相信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
"
我不明白爲什麼你沒有嘗試它,而你已經寫了它的代碼? –
檢查但沒有工作。忘了提及它。 – nixgadgets
啊對不起。我建議你可能喜歡這篇文章:http://www.vogella.com/articles/JavaRegularExpressions/article.html –