Reputation: 688
This sounds like it shuold be something simple, so I think I might be missing something.
Say I want to use a regular expression to match the string "cat", I can do this fine, however, I don't want to match "concatenate" for example, or any other word that contains "cat", I just want "cat" on its own.
How would I achieve this? I considered something like the following:
(CAT|Cat|cat|)( )*
But this doesn't seem to work correctly. I can't add a space on either side in case "Cat" is the first word in the sentence/line.
Is there a way to exclude characters using regular expressions? Something like "Not A-z or 0-9 either side of "cat" "?
Upvotes: 1
Views: 704
Reputation: 785008
You can just use word boundary:
\bcat\b
If you want to disallow certain characters only then use lookarounds:
(?<![a-zA-Z0-9])cat(?![a-zA-Z0-9])
Upvotes: 2
Reputation: 20486
Try word boundaries:
/\bcat\b/gi
A word boundary, \b
, is a zero-length assertion that essentially looks for a word character followed by a non-word character or visa versa (\w\W|\W\w|^\w|\w$
).
Upvotes: 3