我真的不知道如何在 R 中使用不匹配的正则表达式正确查找单词
例如:数据包括:
x = c("hail", "small hail", "wind hail", "deep hail", "thunderstorm hail", "tstm wind hail", "gusty wind hail", "late season hail", "non severe hail", "marine hail")
我想找到所有有“冰雹”但没有“海洋”的观察者
我的尝试:
x[grep("[^(marine)] hail", x)]
-> 我只有 5 个:
"small hail" "wind hail" "deep hail" "tstm wind hail" "gusty wind hail"
不知道其他4个怎么样
最佳答案
使用环视断言。
> x = c("hail", "small hail", "wind hail", "deep hail", "thunderstorm hail", "tstm wind hail", "gusty wind hail", "late season hail", "non severe hail", "marine hail")
> x[grep("^(?=.*hail)(?!.*marine)", x, perl=TRUE)]
[1] "hail" "small hail" "wind hail"
[4] "deep hail" "thunderstorm hail" "tstm wind hail"
[7] "gusty wind hail" "late season hail" "non severe hail"
或者
添加
\b
必要时边界。 \b
单词字符和非单词字符之间的匹配。> x[grep("^(?=.*\\bhail\\b)(?!.*\\bmarine\\b)", x, perl=TRUE)]
^
断言我们处于开始阶段。 (?=.*hail)
正前瞻断言匹配必须包含字符串 hail
(?!.*marine)
否定前瞻断言匹配将不包含字符串 marine
. 关于regex - 正则表达式不包括 R 中的单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27986361/