如何在grep中使用非捕获组? [英] How to use non-capturing groups in grep?

查看:664
本文介绍了如何在grep中使用非捕获组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个答案表明 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中使用非捕获组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆