Why this java regex returning false? -


my intention locate "tokens" , quoted strings string::split() method:

(("[\w\s]*")|(\w*))* 

input text:

this  "a test" abd "and more" 

it returns false.

also, how "quote" can use in source code?

string.split splits using matches separators, i.e. returns doesn't match. if want return you're matching, should use matcher.find.

also, \\w* matches length 0 string, want avoid. use + match 1-or-more.

you have unnecessary brackets , outer * should not there - rest of regex matches characters in single token , multiple tokens should presumably matched across multiple invocations of regex matching (so saying "any number of these" doesn't make sense).

matcher m = pattern.compile("\"[\\w\\s]*\"|\\w+").matcher("this  \"a test\" abd \"and more\""); while (m.find())     system.out.println(m.group()); 

the above prints:

this "a test" abd "and more" 

to remove quotes, can update regex use look-around, check if "'s there, won't match them:

"(?<=\")\\w[\\w\\s]*(?=\")|\\w+" 

to understand how escape things, need keep different layers in mind. first there's java itself, having " start or end string, needs escaped \ if want " character appear in string. there's regex code, expects \w , \s, java doesn't allow \ without being escaped, that's \\w , \\s.


Comments

Popular posts from this blog

cookies - Yii2 Advanced - Share session between frontend and mainsite (duplicate of frontend for www) -

angular - password and confirm password field validation angular2 reactive forms -

php - Permission denied. Laravel linux server -