Regex group is matching quotes when I don't want it to -


i have regular expression:

"([^"\\]|\\.)*"|(\s+) 

regular expression visualization

debuggex demo

but problem is, when have input "foo" , use matcher go through groups, first group finds "foo" when want foo. doing wrong?

edit:

i'm using java , fixed it

"((?:[^"\\]|\\.)*)"|(\s+) 

regular expression visualization

debuggex demo

the first capturing group wasn't including * whole string. enclosed within capturing group , made inner existing 1 non capturing group.

edit: no... it's working in online regex debuggers not in program...

capture contents of double quoted literal pattern (branch 1) , if matched grab it.

also, consider unrolling pattern:

 "([^"\\]*(?:\\.[^\\"]*)*)"|(\s+) 

in java:

string pat = "\"([^\"\\\\]*(?:\\\\.[^\\\\\"]*)*)\"|(\\s+)"; 

note patterns (a|b)* cause stack overflow issue in java, that's why unrolled version preferable.


Comments

Popular posts from this blog

mysql - Dreamhost PyCharm Django Python 3 Launching a Site -

java - Sending SMS with SMSLib and Web Services -

java - How to resolve The method toString() in the type Object is not applicable for the arguments (InputStream) -