【问题标题】:Error in stri_detect_regex in RR中的stri_detect_regex错误
【发布时间】:2018-01-31 09:50:31
【问题描述】:

我收到此错误

stri_detect_regex(string, pattern, opts_regex = opts(pattern)) 中的错误: 正则表达式模式中不正确的嵌套括号。 (U_REGEX_MISMATCHED_PAREN)

当我运行代码时

  # find occurrences of initial dataframe
  named_RN$search <- map_int(named_RN$V1, function(x){sum(str_detect(final_RN$named_RN, pattern = x))})

named_RN$V1 的样子

aldosterone
renin
potassium
calcitrol

final_RN$named_RN 看起来像

aldosterone, creatinine 
human, warfarin
aspirin, renin, calcitrol
magnesium, calcitrol

我的代码旨在在 named_RN 中创建一个新变量,该变量显示每个短语的原始计数,因此 named_RN 看起来像

V1              search
aldosterone     1
renin           0
potassium       0
calcitrol       2

请指教。谢谢。

【问题讨论】:

  • 您在正则表达式模式之一中的括号嵌套不正确。检查例如stringr::str_detect(c("a","a("), c("a", "a("))stringr::str_detect(c("a","a("), stringr::fixed(c("a", "a(")))。也许摆脱括号或使用固定匹配?
  • 如果你使用固定字符串而不是正则表达式匹配,你真的只需要使用str_detect(final_RN$named_RN, fixed(x))
  • @Wiktor,成功了!介意将其发布为答案,以便我接受吗?

标签: r regex dataframe text error-handling


【解决方案1】:

由于您使用的是固定字符串,而不是正则表达式,因此您需要告诉正则表达式引擎将模式用作纯文本。你可以这样使用它:

str_detect(final_RN$named_RN, fixed(x))
                              ^^^^^^^^

"Fixed matches":

fixed(x) 只匹配x 指定的精确字节序列。这是一个非常有限的“模式”,但这种限制可以使匹配更快。

如果您想在执行不区分大小写的搜索时使用人类语言排序规则,也可以考虑使用coll(x)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-02-17
    • 2013-02-08
    • 2013-07-06
    • 2015-11-04
    • 2014-10-22
    • 2015-01-29
    相关资源
    最近更新 更多