正则表达式字边界替代 [英] Regex word boundary alternative
问题描述
我使用的是标准的 \b
字边界.但是,它并没有像我想要的那样处理点 (.) 字符.
所以下面的正则表达式:
\b(\w+)\b
将匹配 cats.dog
中的 cats
和 dogs
如果我有一个字符串表示 cats and dogs don't make猫.狗
.
我需要一个词边界替代方案,仅在以下情况下才匹配整个词:
- 它不包含点(.)字符
- 每边至少用一个空格( )字符封装
有什么想法吗?!
附言我需要这个用于 PHP
你可以尝试使用 (?<=\s)
before 和 (?=\s)
> after 代替 \b
以确保它前后有一个空格,但是您可能还希望允许使用 位于字符串的开头或结尾>(?<=\s|^)
和 (?=\s|$)
这将自动排除带有 .
的单词",但它也会排除句尾的单词,因为它和句号之间没有空格.>
I was using the standard \b
word boundary. However, it doesn't quite deal with the dot (.) character the way I want it to.
So the following regex:
\b(\w+)\b
will match cats
and dogs
in cats.dog
if I have a string that says cats and dogs don't make cats.dogs
.
I need a word boundary alternative that will match a whole word only if:
- it does not contain the dot(.) character
- it is encapsulated by at least one space( ) character on each side
Any ideas?!
P.S. I need this for PHP
You could try using (?<=\s)
before and (?=\s)
after in place of the \b
to ensure that there is a space before and after it, however you might want to also allow for the possibility of being at the start or end of the string with (?<=\s|^)
and (?=\s|$)
This will automatically exclude "words" with a .
in them, but it would also exclude a word at the end of a sentence since there is no space between it and the full stop.
这篇关于正则表达式字边界替代的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!