开发者

How to find a word containing bracket "(" and using word boundaries?

I am looking for a phrase "in successes (and learn from their failures!)" in a 开发者_如何学Cbig text. Since there are brackets i have used quote(...) to allow it, but i also want to use word boundaries "\b" so this phrase will be ignored if it was found in such text "Kin successes (and learn from their failures!)".

Here is my code :

String phrase = Pattern.quote( "in successes (and learn from their failures!)" );   
Pattern myPattern = Pattern.compile( "\\b" + phrase + "\\b" );  // Use word boundary(\b) = No letters after it.
Matcher myMatcher = myPattern.matcher( bigText );
myMatcher.find();  // Returns false.

As mentioned this code will return false because of using "\b". If i omitted "\b" the matcher will return true. Is there a way to fix this while using the 2 conditions: quote(...) + "\b" ?


The best option here in my opinion is to drop the second \b:

Pattern myPattern = Pattern.compile( "\\b" + phrase);

phrase ends with !), which aren't word characters, and should match well for all cases, for example: failures!), and more, failures!), failures!)or not

If you want to make sure it isn't followed by another letter you can use "\\b" + phrase + "\\B", but I'm not sure it's needed here.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜