我有这个表达
\b[A-Za-z]+\b
Run Code Online (Sandbox Code Playgroud)
如果我放弃abc@de mnop,它匹配abc,de和mnop,但我希望它只匹配mnop.我怎样才能做到这一点?
Ani*_*dha 10
\b 是一个单词边界.
所以,\b类似于[^a-zA-Z0-9_]ie \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(^)
Run Code Online (Sandbox Code Playgroud)
\b手段(?:(?<!\w)(?=\w)|(?<=\w)(?!\w)).这将匹配字母和之间的位置@.
你可以写:
(?<!\S)[A-Za-z]+(?!\S)
Run Code Online (Sandbox Code Playgroud)
(?!\S)相当于(?=\s|$).