带有汉字的正则表达式

Regex with Chinese characters

I'm searching text_ which is: 本周(3月25日-3月31日),国内油厂开机率继续下降,全国各地油厂大豆压榨总量1456000吨(出粕1157520吨,出油262080吨),较上周的...[continued]

  crush <- str_extract(string = text_, pattern = perl("(?<=量).*(?=吨(出粕)"))
  meal <- str_extract(string = text_, pattern = perl("(?<=粕).*(?=吨,出)"))
  oil <-  str_extract(string = text_, pattern = perl("(?<=出油).*(?=吨))"))

prints

[1] "1456000"   ## correct
[1] "1157520"   ## correct
[1] NA          ## looking for 262080 here

Why do the first two match but not the last one? I'm using the stringr library.

试试这个:

  oil <-  str_extract(string = text_, pattern = perl("(?<=出油).*(?=吨),较上周的))"))

因为简单以后可能会再次出现你的文字,无法精确定位是哪一部分,可能会超出数据长度或导致数据类型问题。

出于某种原因,我仍然不知道,我无法使用@WiktorStribiżew 的评论解决方案,但这最终奏效了:

oil <-  str_extract(string = text_, pattern = perl("(?<=吨).*(?=吨)"))
# [1] "(出粕1157520吨,出油262080吨),较
oil <- str_extract(string = oil, pattern = perl("(?<=油)\d+(?=吨)"))
# [1] 262080

请注意,当前版本的 stringr 软件包基于 ICU 正则表达式库,不推荐使用 perl()

请注意,lookbehind 模式是固定宽度的,ICU 解析您的 lookbehind 模式中的第一个字母的方式似乎存在问题(由于某些未知原因,它无法计算其宽度)。

由于您使用的是 stringr,您可能只需要依靠 捕获 来提取 ,这可以通过 str_match 实现模式的部分

> match <- str_match(s, "出油(\d+)吨")
> match[,2]
[1] "262080"

这样,您将避免将来出现任何最终问题。此外,这些正则表达式的执行速度更快,因为在搜索字符串的每个位置执行的模式中没有未锚定的后视。

此外,您可以只使用您的 PCRE 正则表达式,基数为 R:

> regmatches(s, regexpr("(?<=出油)\d+(?=吨)", s, perl=TRUE))
[1] "262080"

你检查过你的ICU版本了吗。我之前遇到过这个问题,当时stringi的ICU版本是55,我尝试用ICU 58重新编译stringi,然后stringr可以正常使用汉字了。现在stringi的新版本是用60以上的ICU编译的,问题应该解决了。