正则表达式空白字边界

时间:2013-03-24 17:20:37

标签: regex

我有这个表达

\b[A-Za-z]+\b

如果我提供abc@de mnop,则会与abcdemnop匹配,但我希望它仅匹配mnop。我怎么能这样做?

3 个答案:

答案 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是锚点,根本不是边界

相关问题