in reply to Conditionals within Regex

Restructuring to allow the logic to reside outside the RE isn't a bad idea... but I know it's not what you want. That said, I don't think your RE is bad at all. (How offensive could it be, given what it does?)

But you can clean it up a little:

print "match\n" if /(\d{1,3})%(?(?{$1>91}).?|(?!.?))/;

I think .? is probably better than .* and you don't need the (?: ) construct around your "yes-pattern" in the conditional.

-sauoq
"My two cents aren't worth a dime.";

Replies are listed 'Best First'.
Re: Re: Conditionals within Regex
by jlf (Scribe) on Aug 09, 2002 at 21:27 UTC
    I suppose the only reason to have the logic inside the regex is the case addressed by the (?(condition)yes-pattern|no-pattern) construct, namely where there is additional matching to be done which is dependant on the outcome of the test. In my example there was no such dependency so as you noted it makes sense for the logic to be external to the regex.

    Thanks for helping me figure this out!

    Josh