Regex before match
-
If I have string
1213 494 4 toobad boys 1234 4444
I want to match "bad boys" and everything before and after bad boys until word boundary \b. Something like this
"\\b.+?bad boys.+?\\b"
This returns
1213 494 4 toobad boys
and I want it to match "toobad boys" I know that the problem is "\\b.+?bad" which matches everything up to "bad boys", but instead it has to match everything before "bad boys" until word boundary \b. Help?
-
If I have string
1213 494 4 toobad boys 1234 4444
I want to match "bad boys" and everything before and after bad boys until word boundary \b. Something like this
"\\b.+?bad boys.+?\\b"
This returns
1213 494 4 toobad boys
and I want it to match "toobad boys" I know that the problem is "\\b.+?bad" which matches everything up to "bad boys", but instead it has to match everything before "bad boys" until word boundary \b. Help?
The problem is that your first
\b
is matching too early. Try something like[A-Za-z]*bad boys[A-Za-z]*
In words, that will match "none or some alphabetic characters (no spaces!)" then "bad boys" then "none or some alphabetic characters"Software rusts. Simon Stephenson, ca 1994. So does this signature. me, 2012
-
The problem is that your first
\b
is matching too early. Try something like[A-Za-z]*bad boys[A-Za-z]*
In words, that will match "none or some alphabetic characters (no spaces!)" then "bad boys" then "none or some alphabetic characters"Software rusts. Simon Stephenson, ca 1994. So does this signature. me, 2012
-
Does \w* or \pL* or \p{L}* or (\p{L}|\p{M})* or \p{Word}* do what you want? Different RegExps support different features. See, for example, Regex Tutorial - Unicode Characters and Properties[^]