0

I am trying to understand the correct use of »atomic grouping« a.k.a. »independent subexpressions« a.k.a. »non-backtracking subpattern« [this are the terms I found for the same thing, which doesn't make it less complicate]

  • egrep '123(?>fruit|juiceomatic)machine' means: look for 123fruitjuiceomaticmachine
  • BUT stop trying to match [the rest of] the round brackets' content if 123 is followed by anything else than fruit!

Is this correct? Did I miss something important?

I ask this because I got to the expected results most of the time but I am not 100% sure if this is because I did it right or I just made lucky guesses.

  • 1
    That is not supported by ERE (egrep). If your grep supports -P (PCRE), you can use that. – jordanm Apr 15 '13 at 22:34
  • @jordanm I get quite the results I am looking for without using -P. I'm on a GNU/Linux [Debian based] OS. Could this be connected to GNU ERE? – erch Apr 15 '13 at 22:43
1

This is not an extended regular expression (ERE). egrep will interpret this as 123 followed by either >fruit or juiceomatic (the ? is ignored) followed by machine. This means it will match on:

123>fruitmachine
123>juiceomaticmachine

but not:

123fruitjuiceomaticmachine

The (?>...) notation is from PCRE and means non-capturing atomic group, see pcresyntax(3). You can use those with GNU grep and the -P switch.

If I understand you correctly you require 123 to be followed by fruit which in turn may be followed by juiceomatic or nothing. In that case I think 123fruit(|juiceomatic)machine is the ERE you want.

  • So this command is connected to PCRE; thus the -P option is needed, as @jordanm already explained. This might also explain why I found so much examples in Perl [the Language itself] but nearly non within grep :) What I gave as an example wasn't what I was actually looking for, but an example I came up with myself after learning the difference between Basic [BRE) and Extended Regular Expressions [ERE] the hard way. – erch Apr 16 '13 at 13:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.