Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

Re: Progressive pattern matching

by tommyw (Hermit)
on Oct 14, 2001 at 04:59 UTC ( #118723=note: print w/replies, xml ) Need Help??

in reply to Progressive pattern matching

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 :-(

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://118723]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (8)
As of 2020-06-06 12:51 GMT
Find Nodes?
    Voting Booth?
    Do you really want to know if there is extraterrestrial life?

    Results (41 votes). Check out past polls.