In regex, mystery Error: assertion 'tree->num_tags == num_tags' failed in executing regexp: file 'tre-compile.c', line 634

In regex, mystery Error: assertion 'tree->num_tags == num_tags' failed in executing regexp: file 'tre-compile.c', line 634

假设使用管道分隔符将 900 多个公司名称粘贴在一起形成正则表达式模式 -- "firm.pat".

firm.pat <- str_c(firms$firm, collapse = "|")

对于名为 "bio" 的数据框,它有一个名为 "comment" 的大字符变量(250 行,每行 100 多个单词),我想用空格替换所有公司名称。 gsub 调用和 str_replace_all 调用 return 同样的神秘错误。

bio$comment <- gsub(pattern = firm.pat, x = bio$comment, replacement = "")
Error in gsub(pattern = firm.pat, x = bio$comment, replacement = "") : 
  assertion 'tree->num_tags == num_tags' failed in executing regexp: file 'tre-compile.c', line 634

library(stringr)
bio$comment <- str_replace_all(bio$comment, firm.pat,  "")
Error: assertion 'tree->num_tags == num_tags' failed in executing regexp: file 'tre-compile.c', line 634

traceback()没开导我

> traceback()
4: gsub("aaronson rappaport|adams reese|adelson testan|adler pollock|ahlers cooney|ahmuty demers|akerman|akin gump|allen kopet|allen matkins|alston bird|alston hunt|alvarado smith|anderson kill|andrews kurth|archer 

# hundreds of lines of company names omitted here

lties in all 50 states and washington, dc. results are compiled through a peer-review survey in which thousands of lawyers in the u.s. confidentially evaluate their professional peers."
       ), fixed = FALSE, ignore.case = FALSE, perl = FALSE)
3: do.call(f, compact(args))
2: re_call("gsub", string, pattern, replacement)
1: str_replace_all(bio$comment, firm.pat, "")

其他三个帖子提到了 SO 上的神秘错误,a passing reference and cites two other oblique references,但没有讨论。

我知道这个问题缺少可重现的代码,但即便如此,我如何找出错误的解释?更好的是,我如何避免抛出错误?少数公司似乎不会出现该错误,但我无法检测到模式或阈值。我是运行 Windows 8、RStudio,每个包的更新版本。

谢谢。

我对由数百个制造商名称组成的模式有同样的问题。我可以建议模式太长,所以我将它分成两个或多个模式并且效果很好。

  ml<-length(firms$firm)
  xyz<-gsub(sprintf("(*UCP)\b(%s)\b", paste(head(firms$firm,n=ml/2), collapse = "|")), "", bio$comment, perl=TRUE)
  xyz<-gsub(sprintf("(*UCP)\b(%s)\b", paste(tail(firms$firm,n=ml/2), collapse = "|")), "", xyz, perl=TRUE)

您可以在 qdap 包中使用 mgsub,它是 gsub 的扩展,用于处理模式向量和替换。

请参考这个Answer