2

My sample string is

Indian India's India.

I want to match only "India" as a whole word. My regular expression is /\b(india)\b/i.

But this is giving me 2 matches. one in "India's" and other in last word.

To avoid getting India in India's, I used this regex /\b(india)[^'a-z]\b/i. But this regex is matching "India." (Notice the dot)

I only want to match India. How can I achieve this?

I am doing this in PHP.

Barcenal
  • 252
  • 3
  • 12

2 Answers2

3

You can use the following regex

\bIndia(?=$|\s)
  • (?=$|\s) Positive Lookahead $ assert position at end of a line or \s match any white space character [\r\n\t\f ]

If you want to allow ,(comma) or .(dot) then you can simply use

\bIndia(?=[.,]|$|\s)

Regex

Narendrasingh Sisodia
  • 21,247
  • 6
  • 47
  • 54
3

You need to just modify the regex:

\b(India)\b(?!\')
Mayur Koshti
  • 1,794
  • 15
  • 20