How to use non-capturing groups in grep?
Asked Answered
U

1

60

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?

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
...
Uncinus answered 28/2, 2013 at 13:7 Comment(3)
Can you show some sampel input/output ?Verile
@sputnick I edited the question to add more details.Uncinus
you should've included the contents of the file that you're grepping, before showing what you expect or what you getNeilla
D
95

"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
Dyewood answered 28/2, 2013 at 13:18 Comment(8)
Note that all lookbehinds are zero widthNeilla
Lookbehinds need to be fixed length. In this case it helps, but it doesn't in others.Product
For references, (?<=…) is positive lookbehind assertion, (?<!…) is negative lookbehind assertion, (?=…) is positive lookahead assertion, and (?!…) is negative lookahead assertion. More here.Flat
congrats on gold grep!Curriery
Is there any variant of this that would handle variable width? E.g. if instead of the dot, I had an unknown number of spaces at the end of the lookbehind and wanted to do grep -Po "(?<=syntaxHighlighterConfig +)[a-zA-Z]+Color" file. Right now I get the complaint grep: lookbehind assertion is not fixed length. Thanks.Constant
@Constant You'll probably want to use vim or Python or something if you want to be able to do it all with a regex. If you're just looking to get rid of some spaces after you've captured the main thing, you should be able to remove them in a loop after running grep.Mucoid
Your lookbehind assertion cannot be variable length. Your lookbehind assertion is this part: "(?<=syntaxHighlighterConfig +)". Putting the plus there says "one or more of the preceding character", i.e. the space. You just can't have a lookbehind assertion that could turn out to be more than one length. Maybe grep for just one space there, and then pass the result through 'tr' or 'sed' to remove the spaces.Doris
You could use \K as a variable length negative lookbehind... Keep the stuff left of \K.Curie

© 2022 - 2024 — McMap. All rights reserved.