0

I am using

^(?i)(?=.*\bWORD_TO_FIND\b).*$ 

In this specific case I am trying to match "S.A.M."

This way it works:

^(?i)(?=.*\bs.a.m\b).*$ 

This way it doesn't:

^(?i)(?=.*\bs.a.m.\b).*$ 

Why does that fullstop brakes the match?

1
  • You don't need the lookahead assertion for this. And you should regex quote the find word(s). And you should use a boundary with qualification. You could use a conditional boundary "(?i)^.*(?(?=\\w)\\b)(?:" + regexQuote(text) + ")(?(?<=\\w)\\b).*$" or, the best way is to use a whitespace boundary "(?i)^.*(?<!\\S)(?:" + regexQuote(text) + ")(?!\\S).*$" Commented Nov 18, 2015 at 17:10

1 Answer 1

1

That is because of \b or word boundary.After . there is no \b or word boundary but after m there is.

^(?i)(?=.*\bs\.a\.m\.\b).*$ ^^ Here no word boundary so assertion or lookahead fails. 

You should also be escaping . if you want to match .

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks, what other symbols should I escape?
@user2468425 all metacharacters that have special meaning in regex like .*?{} etc.
@user2468425 do accept if it worked for you
I'm sorry but it doesn't work for me
@user2468425 what doesnt work?see here regex101.com/r/hE4jH0/13 for java you need to use ^(?i)(?=.*\\bs\\.a\\.m\\b).*$

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.