简体   繁体   English

在R中使用regexpr,具有多个字符串肯定和否定

[英]Using regexpr in R with multiple strings affirmation and negation

I am grepping at a column of notes. 我正在看一栏笔记。 Looking for the presence of some strings and the absence of others. 寻找一些字符串的存在和其他人的缺席。 The expression looks like 表达式看起来像

toMatch <- c("words", "i", "want", "to")
notToMatch <- c("not", "in", "my", "res")
insert <- paste(paste(toMatch, collapse="|"), "!", paste(notToMatch, collapse="!"), sep="")
regexpr(insert, df$notes, ignore.case=T)

It seems to me that regexpr will count 在我看来,regexpr将会重要

printNotes = +1 presence and -1 absence

and if that expression evaluates to printNotes > 0, it returns a value other than -1 (which in regexpr indicates not found). 如果该表达式的计算结果为printNotes> 0,则返回-1以外的值(在regexpr中表示未找到)。

Any suggested syntax for regexpr to return -1 if any of the notToMatch "!" regexpr的任何建议语法,如果任何notToMatch“!”返回-1 arguments return TRUE? 参数返回TRUE?

Thanks much! 非常感谢!

You can use grepl() to get a logical vector of where the strings have matched and then sum() that vector to see the number which are matches. 您可以使用grepl()来获取字符串匹配位置的逻辑向量,然后使用sum()向量来查看匹配的数字。 You can do the same thing (roughly) with grep() and counting the length of the resultant vector but grepl() behaves a bit more consistently. 你可以用grep()粗略地做同样的事情并计算结果向量的长度,但是grepl()表现得更加一致。

If you want to get the inverse of any match you can do !grepl("match", x) and it will show the logical inverse. 如果你想得到你可以做的任何匹配的倒数!grepl("match", x)它将显示逻辑逆。

If you *specifically want it to return TRUE or ! 如果你*特别希望它返回TRUE! you can do something like ifelse(grepl("m", letters), TRUE, "!") which searches the letters constant (all 26 lower-case english letters) for "m" and returns TRUE on a match and "!" 你可以做一些像ifelse(grepl("m", letters), TRUE, "!")这样的搜索letters常量(所有26 ifelse(grepl("m", letters), TRUE, "!")英文字母)为“m”并在匹配时返回TRUE并且“!” on a failure to match. 在未能匹配。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM