Reputation: 2028
Can someone supply me with a regex to match a search term that is not preceded or followed by [a-z]
and [A-Z]
? (Other characters are OK.) I.e., when searching for key
, I don't want keyboard
in my search results, but key.
is okay.
Upvotes: 3
Views: 2323
Reputation: 1
No need for the OR
s if you do it like this:
(^|[^A-Za-z])key([^A-Za-z]|$)
Upvotes: 0
Reputation: 3035
As this question is tagged with mysql I assume you are using MySQL regexps. Then [[:<:]]key[[:>:]]
is what you want. See the documentation at dev.mysql.com for details.
Upvotes: 3
Reputation: 2902
If you're using Perl, what you need is \b
, aka "word boundary":
m/\bkey\b/
Upvotes: 1
Reputation: 882028
Since you don't specify what regex engine you're using, I'll assume a baseline, in which case "[^A-Za-z]key[^A-Za-z]
" would be sufficient.
If you also want to catch the string at the start and end of the line, you'll also have to use "^key[^A-Aa-z]
" and "[^A-Aa-z]key$
".
Upvotes: 7