Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: Progressive pattern matching

by tommyw (Hermit)
on Oct 14, 2001 at 04:59 UTC ( [id://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?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (3)
As of 2024-03-29 15:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found