Java: regular expressions escaping regular expressions
This sample data is returned by the web service
I want to split them with split (",") and try to see the results with simple code
String loc = "200,\"California,USA\""; String[] s = loc.split(","); for(String f : s) System.out.println(f);
Unfortunately, this is the result
200 6 "California USA"
The expected result should be
200 6 "California,USA"
I tried different regular expressions and had no luck Is it possible to escape the given regular expression in ''?
Update 1: added c# code
Update 2: deleted c# code
Solution
,(?=(?:[^"]|"[^"]*")*$)
,(?=(?:[^"]|"[^"]*")*$)
This is the regular expression you want (to put it in the split function, you need to escape the quotation marks in the string)
explain
You need to find all ',' not in quotes That's what you need to predict( http://www.regular-expressions.info/lookaround.html )To see if the currently matched comma is within quotation marks or within quotation marks
To do this, we use lookahead to basically ensure that the current match '' is followed by an even number of '' characters (meaning it is outside quotation marks)
So (? [^ "] | [^"] * ") * $means a quotation mark that matches only at the end of a non quotation mark character or has any quotation marks between them
(? = (?: [^ "] |" [^ "] *") * $) will foresee the above competition
, (? = (?: [^ "] |" [^ "] *") * $) finally, this will match all ',' with the above forward-looking