good chemistry is complicated, and a little bit messy -LW |
|
PerlMonks |
Re: Progressive pattern matchingby tommyw (Hermit) |
on Oct 14, 2001 at 04:59 UTC ( [id://118723]=note: print w/replies, xml ) | Need Help?? |
If I'm understanding this right, try: print "$1\n" if /(a?b?c?d?e?)/; For your more specific example: print "$1\n" if /((?:a|b)?(?:b|c)?(?:c|d|e)?)/; or, more simply, print "$&\n" if /(a|b)?(b|c)?(c|d|e)?/;, provided you can stand the dreaded $&These return the first occurance of a matching string. To find the longest string, I'd add a /g modifier, and sort the results according to length. Don't see how to make that work with $& though? Update:It was late, and I'd been down the pub. Not a good time to write code... I missed all the the ? counts out of the regexps after the first, simplest one. My bad. Worse, I've just realised that the simple version will, of course, match ace as well as any of the prefixes from abcde. Which is not what was wanted. My very bad :-(
In Section
Seekers of Perl Wisdom
|
|