regex - String delimiter using Java Pattern -
i have string id delimit using java pattern. there carriage return character after first line. delimiter character |
msh|^~\&|unicare^hl7cisinv10.00.16^l||iba||||adt^a03|3203343722|p|2.3.1||||| evn|a03
i used following code.
pattern pattern = pattern.compile("([^|]++)*"); matcher matcher = pattern.matcher(str); while (matcher.find()) { system.out.println("result: \"" + matcher.group() + "\""); }
doing shows empty characters each of delimiter character. find ignore these. chance of modifying regex characters can ignored.
thanks in advance.
i believe string#split()
simpler 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("\\|+"); (string s : ss) { system.out.println(s); }
output:
msh ^~\& unicare^hl7cisinv10.00.16^l iba adt^a03 3203343722 p 2.3.1 <--- there \r\n in string @ point evn a03
if wanna go using pattern
, can use 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("\\|+"); (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 same 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 "
Comments
Post a Comment