0

I've got a regular expression "(?i).*guitar.*" but how do I make this only match if the word is not surrounded by other characters?

So I thought, let's just add [^a-zA-Z] to both ends of the regular expression except when I did that, it no longer matches when the word is at the beginning or very end of a string.

Ed Lee
  • 187
  • 2
  • 13

1 Answers1

1

Use \b to match at word boundaries:

.*\bguitar\b.*

This will match "I play my guitar every day", but not "I have a subscription to guitarist magazine".

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523