单词边界正则表达式问题
Word boundary regex issue
我在正则表达式中使用单词边界 \b
时遇到问题。我正在使用 R,但是当我尝试 http://regexr.com 时问题也存在。我使用的模式是 \bs\.l\.\b
,虽然我希望下面的第 1 行和第 3 行匹配此模式,但只有第 2 行匹配:
aaa s.l. bbb
aaa s.l.bbb
aaa s.l., bbb
.
不是单词字符,因此 .
字符与 space 或逗号之间没有单词边界。
word boundaries匹配以下位置:
- Before the first character in the string, if the first character is a word character.
- After the last character in the string, if the last character is a word character.
- Between two characters in the string, where one is a word character and the other is not a word character.
现在,您要匹配前面有单词边界但后面没有单词 char 的 s.l.
。您需要将尾随 \b
替换为 (?!\w)
环视:
\bs\.l\.(?!\w)
如果您使用的是基本 R 函数,请使用 perl=TRUE
,它将像在由 ICU 正则表达式库提供支持的 stringr 函数中一样工作。
我在正则表达式中使用单词边界 \b
时遇到问题。我正在使用 R,但是当我尝试 http://regexr.com 时问题也存在。我使用的模式是 \bs\.l\.\b
,虽然我希望下面的第 1 行和第 3 行匹配此模式,但只有第 2 行匹配:
aaa s.l. bbb
aaa s.l.bbb
aaa s.l., bbb
.
不是单词字符,因此 .
字符与 space 或逗号之间没有单词边界。
word boundaries匹配以下位置:
- Before the first character in the string, if the first character is a word character.
- After the last character in the string, if the last character is a word character.
- Between two characters in the string, where one is a word character and the other is not a word character.
现在,您要匹配前面有单词边界但后面没有单词 char 的 s.l.
。您需要将尾随 \b
替换为 (?!\w)
环视:
\bs\.l\.(?!\w)
如果您使用的是基本 R 函数,请使用 perl=TRUE
,它将像在由 ICU 正则表达式库提供支持的 stringr 函数中一样工作。