我有这个表达
\b[A-Za-z]+\b
如果我提供abc@de mnop
,则会与abc
,de
和mnop
匹配,但我希望它仅匹配mnop
。我怎么能这样做?
答案 0 :(得分:9)
\b
是一个单词边界。
因此,\b
类似于[^a-zA-Z0-9_]
,\b
检查除了word
您可以使用此正则表达式
(?<=\s|^)[a-zA-Z]+(?=\s|$)
-------- --------- ------
| | |->match only if the pattern is followed by a space(\s) or end of string/line($)
| |->pattern
|->match only if the pattern is preceded by space(\s) or start of string\line(^)
答案 1 :(得分:6)
\b
表示(?:(?<!\w)(?=\w)|(?<=\w)(?!\w))
。哪个匹配字母和@
之间的位置。
你可以写:
(?<!\S)[A-Za-z]+(?!\S)
(?!\S)
相当于(?=\s|$)
。
答案 2 :(得分:0)
Regex word boundary doen't match(\b) matching and whitespace
样本中只有白色
abc@de mnop
^
尝试\s([A-Za-z]+)\b
其中\s
是锚点,根本不是边界