Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: regex capturing problem

by erikharrison (Deacon)
on Mar 21, 2002 at 13:33 UTC ( [id://153313]=note: print w/replies, xml ) Need Help??


in reply to regex capturing problem

You can't use + on parens like that to "remember" any number of matches - that's one of the reasons to have split. As for why it only gives the last part, is that matches are greedy (unless you tell them not to be) and will get the largest match they can - here the end of your string.

Cheers,
Erik

Replies are listed 'Best First'.
Re: Re: regex capturing problem
by demerphq (Chancellor) on Mar 21, 2002 at 15:16 UTC
    Although I suspect you know, for anybody else reading this a slight clarification:

    is that matches are greedy (unless you tell them not to be) and will get the largest match they can

    This is not strictly true with perls NFA based regex engine. They will match the leftmost longest match that they can. This doesnt mean the longest possible match as a DFA based regex engine (egrep) would provide. Thus

    "AAABBBBBBBBAAAAAAAAAAAA"=~m/A+|A+B+A+/;
    Will match "AAA" and not the entire string. But a DFA based regex engine would match the entire string.

    OTOH reversing the option

    "AAABBBBBBBBAAAAAAAAAAAA"=~m/A+B+A+|A+/;
    Would match the entire string using either engine.

    Yves / DeMerphq
    ---
    Writing a good benchmark isn't as easy it might look.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (4)
As of 2024-04-25 17:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found