John M. Dlugosz has asked for the wisdom of the Perl Monks concerning the following question:
But it's less than elegant in the code. Everything has a prefix char, and here's one that has a postfix as well.
Consider how to simplify this regex:
That is, what I really want is/(^([$@*%]))|(^<.*>$)/
with the stipulation that the $3 is present only if $1 was <, and absent otherwise./^([$@*%<)(.*)(>?)$/
I can certainly do it in a line or 3, especially with lots of comments for clarity. But I'm wondering if there is a really cool way to do it in one succinct bite (wishing for grammars like Perl6 here...).
On a more meditative note, I see that a more powerful pattern engine can make things clearer simply because you can, in analogy with English, sum up your selection with a simple statement of intent, rather than groping for an adjective but having to speak at length about this and that special case.
I see this in documentation, too. If the rule is simple it not only makes the code simple, but makes the documentation easy too.
—John
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: conditional match in regex
by tachyon (Chancellor) on Nov 04, 2002 at 21:45 UTC | |
Re: conditional match in regex
by jryan (Vicar) on Nov 04, 2002 at 23:00 UTC | |
by petral (Curate) on Nov 05, 2002 at 01:23 UTC | |
by jryan (Vicar) on Nov 05, 2002 at 21:47 UTC | |
by petral (Curate) on Nov 05, 2002 at 23:08 UTC | |
by jryan (Vicar) on Nov 06, 2002 at 08:09 UTC | |
| |
by John M. Dlugosz (Monsignor) on Nov 05, 2002 at 16:31 UTC | |
by jryan (Vicar) on Nov 05, 2002 at 19:56 UTC |