String.split(pattern) throws exception due to { in pattern: java.util.regex.PatternSyntaxException -
i have string in java. here part of i'm concerned {3: {108:tr2011052300088}}
later on split on {3: {108:. reason (i've been googling) { , } special character has escaped \} , \{ (clearly doesn't work -> compile time error).
others mention bug in java regex. i'm not sure really. exception is:
exception in thread "main" java.util.regex.patternsyntaxexception: unclosed counted closure near index 2 {3:{108: @ java.util.regex.pattern.error(unknown source)
long story short, code splits string using {3: {108: separator , crashes on it:
string query="{3: {108:"; string [] messageparts = message.split(query); i aware of other ways it, albeit more complicated, writing own parser , such.
how can string split , not have crash?
edit: answer comments:
- double slashes don't help: \\{ give \{3:\{108:mamabearid123}} since 2 slashes become 1
- escaping 1 slash won't compile: invalid escape sequence
example escape {:
public static void main(string[] args) { string s = "{3: {108:tr2011052300088}}"; string[] ss = s.split("\\{3: \\{108:"); system.out.println(ss[1]); //prints tr2011052300088}} }
Comments
Post a Comment