我对R中的正则表达式有一个非常特殊的问题:
- grepl("(|^)over","stackoverflow")
- # [1] TRUE
- grepl("(^|)over","stackoverflow")
- # [1] FALSE
- grepl("(^|x|)over","stackoverflow")
- # [1] FALSE
- grepl("(x|^|)over","stackoverflow")
- # [1] FALSE
- grepl("(x||^)over","stackoverflow")
- # [1] TRUE
为什么所有这些表达式都不能评估为TRUE?
POSIX正则表达式实际上应该使所有那些True.看来
Ville Laurikari’s TRE library的
R uses a slightly modified version并不完全符合标准.我会遵循@ rawr的建议,并使用perl = TRUE来获得更兼容的正则表达式.
另见:When both halves of an OR regex group match,is it defined which will be chosen?