问题描述
表明 grep -P
支持(?:pattern)
语法,但它似乎不适用于我(该组仍然被捕获并显示为匹配的一部分)。我错过了什么?
This answer suggests that grep -P
supports the (?:pattern)
syntax, but it doesn't seem to work for me (the group is still captured and displayed as part of the match). Am I missing something?
我正在尝试 grep -oP(?:syntaxHighlighterConfig\。)[a-zA-Z] + Color 上的SyntaxHighlighter.js
并期望结果为:
I am trying grep -oP "(?:syntaxHighlighterConfig\.)[a-zA-Z]+Color" SyntaxHighlighter.js
on this code, and expect the results to be:
wikilinkColor
externalLinkColor
parameterColor
...
但是我得到:
but instead I get:
syntaxHighlighterConfig.wikilinkColor
syntaxHighlighterConfig.externalLinkColor
syntaxHighlighterConfig.parameterColor
...
推荐答案
非捕获并不意味着该组不属于比赛的一部分;这意味着该组的值不会保存用于后退引用。你正在寻找的是一个后视零宽度断言:
"Non-capturing" doesn't mean that the group isn't part of the match; it means that the group's value isn't saved for use in back-references. What you are looking for is a look-behind zero-width assertion:
grep -Po "(?<=syntaxHighlighterConfig\.)[a-zA-Z]+Color" file
这篇关于如何在grep中使用非捕获组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!