Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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.

share|improve this question
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? –  Chirp. Not Luke. Apr 15 '13 at 22:43
add comment

1 Answer

up vote 1 down vote accepted

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.

share|improve this answer
    
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. –  Chirp. Not Luke. Apr 16 '13 at 13:12
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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