使用Regex选择多个句子模式-分组有问题吗?

时间:2019-01-13 16:56:53

标签: r regex regex-group

我要在R中使用Regex语句从数据帧中提取模式的完全匹配项时遇到麻烦。

我有11个句子模式,并且我希望能够使用一个Regex从数据框中仅选择与这些模式匹配的记录作为完全匹配项(我已经使它能够与多个Regex一起使用,但这是一个真实的麻烦)。对于我可以做到这一点的任何帮助,我们将不胜感激。

这些是我的句子:

  • 从任何其他章改变到标题0101至0106。
  • 从任何其他章改变到子目0712.20至0712.39。
  • 从任何其他章改变到标题0903。
  • 从任何其他标题更改为子标题1806.20。
  • 从任何其他章改变至子目1207.99。
  • 从任何其他标题更改为标题4302。
  • 从标题4102或任何其他章改变到子标题4105.10。
  • 从标题4102,子标题4105.10或任何其他章节更改为子标题4105.30。
  • 从副标题4103.10或任何其他章改变到副标题4106.21。
  • 从副标题4103.10或4106.21或任何其他章改变到副标题4106.22。
  • 从税目7304.49或任何其他税目改变至关税项目7304.41.30 其他章节。

这是我现在拥有的正则表达式,它选择完全匹配和部分匹配(卡住的地方)-因此,除了这些句子之外,我最终从数据框中获得了不需要的记录(我知道太乱了,仅举一个例子)。

^A change to (?:headings|heading|subheadings|subheading|tariff item) (?:\d+\S\d+\S\d+|\d+\S\d+) (?:through \d+\S\d+ from any other chapter.|from any other chapter.|from any other heading.|)|from heading \d+\S\d+ or any other chapter.|from (?:heading|subheading|subheadings) \d+\S\d+|, subheading \d+\S\d+ or any other chapter| or any other chapter.| or \d+\S\d+

这是我使用正则表达式可以对所有11个句子进行完全匹配的结果。在此之后,我仍然无法继续保持整洁:

^A change to (?:tariff item|headings|heading|subheading|subheadings) (?:\d+\S\d+|\d+\S\d+\S\d+|\d+\S\d+) (?:from|through) 

enter image description here

1 个答案:

答案 0 :(得分:2)

您可以使用

rx <- "A\\s+change\\s+to\\s+(?:(?:sub)?headings?|tariff\\s+item)\\s+\\d[0-9.]*(?:\\s+through\\s+\\d[0-9.]*)?\\s+from(?:(?:,?\\s+(?:sub)?headings?\\s+\\d[0-9.]*)+(?:\\s+or\\s+\\d[0-9.]*)*\\s+or)?\\s+any\\s+other\\s+(?:heading|chapter)\\."

请参见regex demo。请注意,\s+匹配1个或多个空格字符,即使单词之间的空格数量和类型不是恒定的,也将匹配。

详细信息

  • A\\s+change\\s+to\\s+-A change to 子字符串
  • (?:(?:sub)?headings?|tariff\\s+item)-subheadingsubheadingsheadingheadingstariff item子字符串
  • \\s+\\d[0-9.]*-1个以上的空格,1位数字和0个或多个数字或.
  • (?:\\s+through\\s+\\d[0-9.]*)?-可选序列:
    • \\s+-超过1个空格
    • through-through
    • \\s+-超过1个空格
    • \\d[0-9.]*-1位数字和0个或更多数字或.
  • \\s+from-1个以上的空格和from
  • (?:(?:,?\\s+(?:sub)?headings?\\s+\\d[0-9.]*)+(?:\\s+or\\s+\\d[0-9.]*)*\\s+or)?-可选序列:
    • (?:,?\\s+(?:sub)?headings?\\s+\\d[0-9.]*)+-1个或多个序列:
      • ,?-可选的,
      • \\s+
      • (?:sub)?headings?-可选的sub,然后是heading,然后是可选的s
      • \\s+-超过1个空格
      • \\d[0-9.]*-一个数字,然后是0+数字或.个字符
    • (?:\\s+or\\s+\\d[0-9.]*)*-0个或多个序列:
      • \\s+-超过1个空格
      • or\\s+\\d[0-9.]*-or,1+个空格,一个数字,然后是0+个数字或.个字符
    • \\s+or-1个以上的空格和or
  • \\s+any\\s+other\\s+(?:heading|chapter)\\.- any other heading. any other chapter.

所有11个匹配项均在this online R demo中返回:

text <- "A change to headings 0101 through 0106 from any other chapter.
A change to subheadings 0712.20 through 0712.39 from any other chapter.
A change to heading 0903 from any other chapter.
A change to subheading 1806.20 from any other heading.
A change to subheading 1207.99 from any other chapter.
A change to heading 4302 from any other heading.
A change to subheading 4105.10 from heading 4102 or any other chapter.
A change to subheading 4105.30 from heading 4102, subheading 4105.10 or any other chapter.
A change to subheading 4106.21 from subheading 4103.10 or any other chapter.
A change to subheading 4106.22 from subheadings 4103.10 or 4106.21 or any other chapter.
A change to tariff item 7304.41.30 from subheading 7304.49 or any other chapter."
rx <- "A\\s+change\\s+to\\s+(?:(?:sub)?headings?|tariff\\s+item)\\s+\\d[0-9.]*(?:\\s+through\\s+\\d[0-9.]*)?\\s+from(?:(?:,?\\s+(?:sub)?headings?\\s+\\d[0-9.]*)+(?:\\s+or\\s+\\d[0-9.]*)*\\s+or)?\\s+any\\s+other\\s+(?:heading|chapter)\\."
regmatches(text, gregexpr(rx, text))