in reply to Re^2: Match a pattern only if it is not within another pattern
in thread Match a pattern only if it is not within another pattern

That's "Working as designed".

Would you expect to match ( stuff ) and ) stuff ( with the same regex? How would this be a generalisation?


Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
"Science is about questioning the status quo. Questioning authority".
The "good enough" maybe good enough for the now, and perfection maybe unobtainable, but that should not preclude us from striving for perfection, when time, circumstance or desire allow.

Replies are listed 'Best First'.
Re^4: Match a pattern only if it is not within another pattern
by Transient (Hermit) on Aug 17, 2005 at 17:31 UTC
    Well, technically the OP does say "surrounded", which could mean the boundaries are switched. Your usage of parens in the example is misleading, because parens are inherently related to internal grouping

    But I imagine (not tested) a simple extension of your original regexp could be in order.

    $str =~ s[(bar.+?qux)|(qux.+?bar)|(foo)][defined $3 ? '123' : (defined +($2) ? $2 : $1)]ge;
      Well, technically the OP does say "surrounded", ...

      Actually not.

      And the examples given do not indicate this possibility either.

      Perhaps more important is that "reversible bracketing" doesn't really make much sense. Consider

      foo < foo > foo < foo < foo > foo > foo < foo < foo > foo < foo > foo +> foo 123 |.....|.....| |.....| |.....| |.....|.....|.....| + 123 |.................| |............................. +|

      Can you conceive of an application where this would make sense?


      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
      "Science is about questioning the status quo. Questioning authority".
      The "good enough" maybe good enough for the now, and perfection maybe unobtainable, but that should not preclude us from striving for perfection, when time, circumstance or desire allow.
        Well, technically the OP does say "surrounded", ...
        Actually not.


        Read again:
        # replace all foo(s) above with 123 except for the ones that are # surrounded by bar and qux
        to me it's conceivable that it also meant the reverse of bar.+qux, although it is a stretch. I agree that the examples do not indicate this, however.

        My ability to conceive of an application where reversible bracketing would make sense is irrelevent, but it really does only make sense on a primary, un-nested level, e.g.
        bar foo qux qux foo bar qux foo bar bar foo qux

        whereas these are already covered by the base case:
        bar qux foo bar qux bar foo qux foo bar foo qux
        although the first one could be seen as overlapping nesting and then we get into a whole 'nother realm of questions =) In any case, I agree that, except in extreme cases, your original response would be more than sufficient. I am merely trying to present another possible interpretation.