Reputation: 3523
I am looking for a regex that would indicate if there is a certain complete word in a text, for example for the word "cto"
:
"i am a cto"
- true"i am a cto/developer"
- true"cto"
- true"i am a cto_developer"
- true"ctools"
- falseI want the word to be case insensitive. Any thoughts?
Upvotes: 0
Views: 2081
Reputation: 10080
(?<=^|[^a-zA-Z0-9])<word>(?=$|[^a-zA-Z0-9])
Finds instances of <word>
that are only preceded or succeeded by beginning or end of line/string or characters a-z or numbers 0-9. Example from RegexHero.
It would help if we knew what language this is in.
Upvotes: 4
Reputation: 4916
You could either write an expression considering all the possibilities or use the \b anchor like \bmyWord\b
and consider the _ a lost case because it's handled as part of the word so the "i am a cto_developer" will also return false.
Upvotes: 0