正则表达式在更大的比赛中重复捕获组?
Regex to repeatedly capture group within a larger match?
gedit 中的上下文、语法突出显示。
问题:我想捕获特定区域内的所有事件。玩具示例:
other text here
keyword1 -> (( randomt:,ext ))
keyword1 -> (( randomt:,ext ))
other text here
我想捕获(突出显示)在 keyword1
的 (( text ))
中出现的所有 $0-9(个位数)。 (这里 </code>、<code>
、</code>、<code>
、</code>、<code>
但 不是 </code> 和 <code>
)。这归结为:如何在更大的匹配中重复捕获一个组?
我可以抓取所有可能出现组的文本:(?<=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 不支持它(据我所知)。有什么建议吗?
描述
为了确保您只处理以您的关键字开头的行,我认为这是一个两步操作。
- 收集您感兴趣的每一行
- 提取
$[0-9]
个子字符串
第 1 步
此正则表达式捕获类似于 keyword1 -> ((...))
的行
keyword1\s*->\s*\(\(.*\)\)
第 2 步
$[0-9](?![0-9])(?=(?:(?!\(\().)*\)\))
此正则表达式将执行以下操作:
- 查找
((...))
中所有后跟单个数字的美元符号
例子
现场演示
https://regex101.com/r/wY3jM6/1
示例文本
other text here
keyword1 -> (( randomt:,ext ))
keyword1 -> (( randomt:,ext ))
other text here
样本匹配
说明
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
----------------------------------------------------------------------
gedit 中的上下文、语法突出显示。
问题:我想捕获特定区域内的所有事件。玩具示例:
other text here
keyword1 -> (( randomt:,ext ))
keyword1 -> (( randomt:,ext ))
other text here
我想捕获(突出显示)在 keyword1
的 (( text ))
中出现的所有 $0-9(个位数)。 (这里 </code>、<code>
、</code>、<code>
、</code>、<code>
但 不是 </code> 和 <code>
)。这归结为:如何在更大的匹配中重复捕获一个组?
我可以抓取所有可能出现组的文本:(?<=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 不支持它(据我所知)。有什么建议吗?
描述
为了确保您只处理以您的关键字开头的行,我认为这是一个两步操作。
- 收集您感兴趣的每一行
- 提取
$[0-9]
个子字符串
第 1 步
此正则表达式捕获类似于 keyword1 -> ((...))
keyword1\s*->\s*\(\(.*\)\)
第 2 步
$[0-9](?![0-9])(?=(?:(?!\(\().)*\)\))
此正则表达式将执行以下操作:
- 查找
((...))
中所有后跟单个数字的美元符号
例子
现场演示
https://regex101.com/r/wY3jM6/1
示例文本
other text here
keyword1 -> (( randomt:,ext ))
keyword1 -> (( randomt:,ext ))
other text here
样本匹配
说明
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
----------------------------------------------------------------------