in reply to Re: Regex conditional match if previous match in same expression is true?
in thread Regex conditional match if previous match in same expression is true?

That doesn't give me the desired results at all. I was using Perl 5.6, but $^N was only introduced in 5.8.

By the way, you should localize your package variables whenever possible. Replace
our $paren;
with
local our $paren;

I'd also add a comment along the lines of "Always use package variables with regular expressions." Someone reading or maintaining the code could very well not know that lexical variables can cause problems.

Finally, to avoid continually compiling regexp fragments, replace
(??{$paren ? "\}" : "(?!\})"})
with
(?(?{ $paren }) } | (?!}) )

Replies are listed 'Best First'.
Re^3: Regex conditional match if previous match in same expression is true?
by demerphq (Chancellor) on Apr 10, 2007 at 17:30 UTC

    Note that Rhandom and I basically posted the same pattern, the main difference being mine doesn't need the (?{})/(??{})/$^N stuff, using the conditional pattern instead (as you originally requested).

    Actually to be honest I didnt really look deeply at Rhandom's post before I posted mine. Its cool we came up with the same thing pretty much, but using two different advanced feature sets.

    ---
    $world=~s/war/peace/g

      Aye, I noticed that after I replied. I didn't mean to diminish your post. I basically stopped following the thread after Rhandom posted.