【问题标题】:Regex to repeatedly capture group within a larger match?正则表达式在更大的匹配中重复捕获组?
【发布时间】:2016-05-20 21:00:03
【问题描述】:

上下文,gedit 中的语法高亮显示。

问题:我想捕获特定区域内的所有事件。玩具示例:

other text here $5
keyword1 ->  (( ran$3dom$6t:,ext$9    ))
keyword1  -> ((    ran$2dom$4t:,ext$6 ))
other text here $7

我想在keyword1(( text )) 中捕获(突出显示)所有$0-9(个位数)的事件。 (这里是$3$6$9$2$4$6不是$5$7)。这归结为:如何在更大的比赛中重复捕获一个组?

我可以抓取所有可能出现组的文本:(?<=keyword1)|\(\(.*\)\)(gedit 默认使用 \g)

<context id="keyword1" style-ref="argument">
  <match>(?<=keyword1)|\(\(.*\)\)</match>
</context>

我发现了这个相关问题:How can I write a regex to repeatedly capture group within a larger match? 但该答案在后视中使用了无限重复,不幸的是,gedit 不支持(据我所知)。有什么建议吗?

【问题讨论】:

  • 如果 gedit 支持 PCRE,您可以使用基于 \G 的正则表达式。

标签: regex pcre gedit


【解决方案1】:

说明

为了确保您只处理以关键字开头的行,我认为这是一个两步操作。

  1. 收集您感兴趣的每一行
  2. 提取$[0-9] 子字符串

第一步

这个正则表达式捕获类似于keyword1 -&gt; ((...))的行

keyword1\s*->\s*\(\(.*\)\)

第 2 步

\$[0-9](?![0-9])(?=(?:(?!\(\().)*\)\))

此正则表达式将执行以下操作:

  • 查找((...)) 中存在的所有美元符号后跟单个数字

示例

现场演示

https://regex101.com/r/wY3jM6/1

示例文本

other text here $5
keyword1 ->  (( ran$3dom$6t:,ext$9    ))
keyword1  -> ((    ran$2dom$4t:,ext$6 ))
other text here $7

示例匹配

$3
$6
$9
$2
$4
$6

说明

NODE                     EXPLANATION
----------------------------------------------------------------------
  \$                       '$'
----------------------------------------------------------------------
  [0-9]                    any character of: '0' to '9'
----------------------------------------------------------------------
  (?!                      look ahead to see if there is not:
----------------------------------------------------------------------
    [0-9]                    any character of: '0' to '9'
----------------------------------------------------------------------
  )                        end of look-ahead
----------------------------------------------------------------------
  (?=                      look ahead to see if there is:
----------------------------------------------------------------------
    (?:                      group, but do not capture (0 or more
                             times (matching the most amount
                             possible)):
----------------------------------------------------------------------
      (?!                      look ahead to see if there is not:
----------------------------------------------------------------------
        \(                       '('
----------------------------------------------------------------------
        \(                       '('
----------------------------------------------------------------------
      )                        end of look-ahead
----------------------------------------------------------------------
      .                        any character
----------------------------------------------------------------------
    )*                       end of grouping
----------------------------------------------------------------------
    \)                       ')'
----------------------------------------------------------------------
    \)                       ')'
----------------------------------------------------------------------
  )                        end of look-ahead
----------------------------------------------------------------------

【讨论】:

  • 感谢您(解释清楚)的回复,正则表达式确实捕获了所有必需的 $[0-9] 出现,但它没有考虑到“keyword1”。如果我将第二个关键字 1 替换为关键字 2,您建议的正则表达式将失败(因此它不应捕获 $2、$4 和 $6),但确实如此。不幸的是我的伎俩| (请参阅开头的正则表达式)不适用于您的正则表达式。
猜你喜欢
  • 2014-11-13
  • 1970-01-01
  • 2019-09-29
  • 2014-01-30
  • 1970-01-01
  • 1970-01-01
  • 2011-09-19
  • 1970-01-01
相关资源
最近更新 更多