Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex 正则表达式空白字边界_Regex - Fatal编程技术网

Regex 正则表达式空白字边界

Regex 正则表达式空白字边界,regex,Regex,我有这个表情 \b[A-Za-z]+\b 如果我给出abc@demnop,它匹配abc,de和mnop,但我希望它只匹配mnop。我该怎么做?\b是一个单词边界 因此,\b类似于[^a-zA-Z0-9,即\b将检查以外的任何内容 您可以改为使用这个正则表达式 (?<=\s|^)[a-zA-Z]+(?=\s|$) -------- --------- ------ | | |->match only if the pattern is followe

我有这个表情

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

如果我给出
abc@demnop
,它匹配
abc
de
mnop
,但我希望它只匹配
mnop
。我该怎么做?

\b
是一个单词边界

因此,
\b
类似于
[^a-zA-Z0-9
,即
\b
检查
以外的任何内容

您可以改为使用这个正则表达式

(?<=\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(^)
(?仅当模式后跟空格(\s)或字符串/行结尾($)时匹配
||->模式
|->仅当模式前面有空格(\s)或字符串开头(^)时匹配

\b
表示将字母和
@
之间的位置匹配的
(?:(?)

你可以写:

(?<!\S)[A-Za-z]+(?!\S)
(?

(?!\S)
相当于
(?=\S |$)
正则表达式单词边界不匹配(\b)匹配和空白

样品中只有白色是白色的

abc@de mnop   
      ^
试试
\s([A-Za-z]+)\b


其中,
\s
是锚定符,而不是边界

您可以更具体地说明要匹配的内容,可以使用空格和标点符号作为分隔符,而不是单词边界。您需要指定语言/工具。.regex实现因语言而异请选择,因为它更便于移植,因为有些regex风格要求lookbehind模式具有固定长度。@BoristheSpider您有一个可以在JavaScript中运行的版本吗?