正则表达式空白字边界 [英] Regex whitespace word boundary
本文介绍了正则表达式空白字边界的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这个表情
\b[A-Za-z]+\b
如果我给 abc@de mnop
,它匹配 abc
、de
和 mnop
,但我想要它只匹配 mnop
.我该怎么做?
If I give abc@de mnop
, it matches abc
, de
and mnop
, but I want it to match only mnop
. How can I do that?
推荐答案
\b
是一个词边界.
所以,\b
类似于 [^a-zA-Z0-9_]
即 \b
会检查 用于除 word
So, \b
is similar to [^a-zA-Z0-9_]
i.e \b
would check for anything except word
你可以改用这个正则表达式
You can instead use this regex
(?<=\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(^)
这篇关于正则表达式空白字边界的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文