Regular expression for search terms not preceded or followed by [az] and [AZ]

Can someone provide me with a regular expression to match a search term that is not preceded or followed by [a-z]and [a-z]? (Other characters are okay.) Ie, when searching key, I don't want the keyboardsearch results, but key.okay.

+3
source share
6 answers

Since you will not indicate which regex engine you are using, I will take a basic level, in which case " [^A-Za-z]key[^A-Za-z]" will be enough.

If you also want to catch the line at the beginning and end of the line, you will also have to use " ^key[^A-Aa-z]" and " [^A-Aa-z]key$".

+7

\bkey\b , .

\b -

+6

I assume you are using MySQL regexps. Then [[:<:]]key[[:>:]]this is what you want. See the documentation at dev.mysql.com for more information .

+3
source

If you use Perl, you need \baka word boundary:

m/\bkey\b/
+1
source

Or more concise [^\w]key[^\w]

+1
source

There is no need ORif you do it like this:

(^|[^A-Za-z])key([^A-Za-z]|$)
0
source

Source: https://habr.com/ru/post/1699267/


All Articles