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.
有人能为我提供正则表达式以匹配[a-z]和[A-Z]之前或之后的搜索词吗? (其他字符都可以。)即,在搜索密钥时,我不想在搜索结果中使用键盘,而是键。没关系。
6 个解决方案
#1
7
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.
既然你没有指定你正在使用的正则表达式引擎,我会假设一个基线,在这种情况下“[^ A-Za-z] key [^ A-Za-z]”就足够了。
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$
".
如果你还想在行的开头和结尾捕获字符串,你还必须使用“^ key [^ A-Aa-z]”和“[^ A-Aa-z] key $”。
#2
6
\bkey\b
should do what you want.
\ bkey \ b应该做你想要的。
\b
is a word boundary
\ b是单词边界
#3
3
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.
由于这个问题用mysql标记,我假设您正在使用MySQL regexp。然后[[:<:]]键[[:>:]]就是你想要的。有关详细信息,请参阅dev.mysql.com上的文档。
#4
1
Or the more concise [^\w]key[^\w]
或者更简洁的[^ \ w]键[^ \ w]
#5
1
If you're using Perl, what you need is \b
, aka "word boundary":
如果你正在使用Perl,你需要的是\ b,又名“单词边界”:
m/\bkey\b/
#6
0
No need for the OR
s if you do it like this:
如果您这样做,则不需要OR:
(^|[^A-Za-z])key([^A-Za-z]|$)
#1
7
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.
既然你没有指定你正在使用的正则表达式引擎,我会假设一个基线,在这种情况下“[^ A-Za-z] key [^ A-Za-z]”就足够了。
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$
".
如果你还想在行的开头和结尾捕获字符串,你还必须使用“^ key [^ A-Aa-z]”和“[^ A-Aa-z] key $”。
#2
6
\bkey\b
should do what you want.
\ bkey \ b应该做你想要的。
\b
is a word boundary
\ b是单词边界
#3
3
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.
由于这个问题用mysql标记,我假设您正在使用MySQL regexp。然后[[:<:]]键[[:>:]]就是你想要的。有关详细信息,请参阅dev.mysql.com上的文档。
#4
1
Or the more concise [^\w]key[^\w]
或者更简洁的[^ \ w]键[^ \ w]
#5
1
If you're using Perl, what you need is \b
, aka "word boundary":
如果你正在使用Perl,你需要的是\ b,又名“单词边界”:
m/\bkey\b/
#6
0
No need for the OR
s if you do it like this:
如果您这样做,则不需要OR:
(^|[^A-Za-z])key([^A-Za-z]|$)