R中的正则表达式否定
我在试图找到一种在R正则表达式中实现否定的方法时遇到了一个问题R中的正则表达式否定,r,regex,regex-negation,R,Regex,Regex Negation,我在试图找到一种在R正则表达式中实现否定的方法时遇到了一个问题 my_strings <- c("a non-rheumatic fever", "a nonrheumatic fever", "a rheumatic fever", "a not rheumatic fever") my_strings ## [1] "a non-rheumatic fever" "a nonrheumatic fever" "a rheumatic fever" "a not rheumatic fe
my_strings <- c("a non-rheumatic fever", "a nonrheumatic fever", "a rheumatic fever", "a not rheumatic fever")
my_strings
## [1] "a non-rheumatic fever" "a nonrheumatic fever" "a rheumatic fever" "a not rheumatic fever"
我尝试了以下方法,但我不知道如何否定紧跟在风湿病之前的“no(n | t)(\\s+|-)?”
:
t_inc也许我们可以将语法修改为稍微简单一点的语法,方法是使用@IceCreamToucan在评论中提到的invert
grep("no[nt][- ]?rheumatic", my_strings, invert = TRUE, value = TRUE)
#[1] "a rheumatic fever"
模式匹配“否”,后跟字母“n”或“t”,后跟a-或空格(如果存在)和单词“风湿”。使用invert=TRUE
,它将返回所有与模式不匹配的匹配项谢谢。我最初试图将“风湿”作为一个更大的排除术语列表的一部分,但我想我必须将其与此分开。
t_inc <- "\\b([^n][^o][^nt](\\s+|-)?(rheumatic))\\b"
grep(t_inc, my_strings, ignore.case = T, perl = T, value = T)
## character(0)
t_inc <- "\\b([^(no(n|t))](\\s+|-)?(rheumatic))\\b"
grep(t_inc, my_strings, ignore.case = T, perl = T, value = T)
## character(0)
grep("no[nt][- ]?rheumatic", my_strings, invert = TRUE, value = TRUE)
#[1] "a rheumatic fever"