R 帮助文件regex says
符号 \ 分别匹配空字符串
单词的开头和结尾。符号 \b 匹配空字符串
单词的边缘
(单词的)末端和边缘有什么区别?
之间的区别\b
and \<
/ \>
就是它\b
可以在 PCRE 正则表达式模式中使用(当您指定perl=TRUE
) 和 ICU 正则表达式模式 (stringr包裹)。
> s = "no where nowhere"
> sub("\\<no\\>", "", s)
[1] " where nowhere"
> sub("\\<no\\>", "", s, perl=T) ## \> and \< do not work with PCRE
[1] "no where nowhere"
> sub("\\bno\\b", "", s, perl=T) ## \b works with PCRE
[1] " where nowhere"
> library(stringr)
> str_replace(s, "\\bno\\b", "")
[1] " where nowhere"
> str_replace(s, "\\<no\\>", "")
[1] "no where nowhere"
优点\<
(始终代表单词的开头)和\>
(始终匹配单词的结尾)的优点是它们是明确的。这\b
可以匹配两个位置。
还有一件事需要考虑(refrence):
gsub 和 gregexpr 的 POSIX 1003.2 模式在处理重复的字边界时无法正常工作(例如,pattern = "\b"
). Use perl = TRUE
对于此类匹配(但对于非 ASCII 输入可能无法按预期工作,因为“单词”的含义取决于系统)。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)