Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: left-non-greedy regex?

by MarkM (Curate)
on Mar 25, 2003 at 23:00 UTC ( [id://245813]=note: print w/replies, xml ) Need Help??


in reply to left-non-greedy regex?

You seem to misunderstand how regular expressions work. There is no such thing as right-non-greedy or left-non-greedy. There is only "does this expression match?" In the case that it does not, the regular expression engine advances one character and tries again (recursive, and quite a bit more complicated than that).

What you describe as "right-non-greedy" is really just "first match, left-to-right, non-greedy." The exact request you are making seems to be "first match, right-to-left, non-greedy", which is really:

my $str = "tar a rat at ararat"; reverse($str) =~ /ta(.*?)ta/; print scalar(reverse($1)), "\n"; # prints: " arar\n"

Note that not only must the string be matched backwards, but the regexp itself must be backwards. What you are really asking for is a regular expression engine that begins matching from the last position in the string, and matches the regular expression in reverse order. This sort of thing has been talked about for Perl 6, and I think there is at least one CPAN module to automate reversing a regular expression.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (8)
As of 2024-04-18 11:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found