簡體   English   中英

R中的正則表達式否定

[英]Regular expression negation in R

我在嘗試找到一種在 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 fever"

在上面的字符串中,我試圖找到一個僅輸出以下內容的正則表達式:

## [1] "a rheumatic fever"

我嘗試了以下操作,但我不知道如何否定"no(n|t)(\\\\s+|-)?" 緊接在"rheumatic"

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)

請有人能給我一些指點嗎?

也許我們可以通過@IceCreamToucan 在評論中提到的使 ue of invert將語法修改為更簡單的語法

grep("no[nt][- ]?rheumatic", my_strings, invert = TRUE, value = TRUE)
#[1] "a rheumatic fever"

該模式匹配“no”,后跟字母“n”或“t”,后跟“-”或空格(如果存在)和“風濕病”一詞。 使用invert= TRUE ,它將返回所有與模式不匹配的匹配項

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM