在下面的示例中,我希望使用regex匹配一些字符串,同时在R中否定其他字符串,我希望排除否则我想要匹配的字符串的子节。下面的示例使用来自Regular expression to match a line that doesn't contain a word?的答案。
我的困惑是,当我尝试这样做时,grepl会抛出一个错误:
grepl中的错误(mypattern,mystring):无效的正则表达式‘boardgames x(^((?!games).)*$’,原因是‘无效的regexp’
mypattern <- "boardgames|(^((?!games).)*$)"
mystring <- c("boardgames", "boardgames", "games")
grepl(mypattern, mystring)注使用str_detect运行将返回所需的结果(即T、T、F),但我希望使用grepl。
发布于 2017-07-26 15:55:02
我们需要perl = TRUE,因为默认选项是perl = FALSE
grepl(mypattern, mystring, perl = TRUE)
#[1] TRUE TRUE FALSE这是在使用Perl-compatible regexps时需要的
根据?regexp
perl = TRUE参数用于grep、regexpr、gregexpr、sub、gsub和str拆分,切换到PCRE库,该库使用与Perl 5.x相同的语法和语义实现正则表达式模式匹配,但有一些不同之处。
https://stackoverflow.com/questions/45331912
复制相似问题