Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Search a match to a regex from the END of a string

by Doctrin (Beadle)
on Mar 15, 2013 at 10:36 UTC ( #1023659=perlquestion: print w/ replies, xml ) Need Help??
Doctrin has asked for the wisdom of the Perl Monks concerning the following question:

Hello dear Monks! Is there a way to make Perl search matches from the end of a string, not from start (except inversing the string itself)? Thanks

Comment on Search a match to a regex from the END of a string
Re: Search a match to a regex from the END of a string
by Anonymous Monk on Mar 15, 2013 at 10:42 UTC

    How do you mean?
    In Perl is there more than ONE WAY to do it..

Re: Search a match to a regex from the END of a string
by Athanasius (Monsignor) on Mar 15, 2013 at 12:13 UTC

    Like this?

    22:06 >perl -wE "my $s = 'abc1abc2abc3abc4abc5zz'; $s =~ /.*(abc\d)/; +say qq[matched $1] if $1;" matched abc5 22:07 >

    The greedy quantifier in the regex term .* eats up as much of the search string as possible, thereby ensuring that the last match is found.

    Hope that helps,

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Re: Search a match to a regex from the END of a string
by MidLifeXis (Prior) on Mar 15, 2013 at 13:07 UTC

    ISTR seeing a typical response to this query being to reverse the string and search for the reversed pattern. Is there a reason that is restricted from your solution set?

    Beyond that, is there some more information that you can give that may help provide a solution within the problem domain you are approaching? It can be easier to find a solution for a specific problem set than for the entire generalized problem set.

    --MidLifeXis

Re: Search a match to a regex from the END of a string
by flexvault (Parson) on Mar 15, 2013 at 14:17 UTC

    Doctrin,

    And there is 'rindex' which is very fast and easy to use.

    perl -e '$d="/usr/local/bin/perl"; $i=rindex($d,"\/"); print substr +($d,$i+1),"\n";'
    which prints 'perl'. The Camel book has examples of how to do recursive searches.

    Good Luck...Ed

    "Well done is better than well said." - Benjamin Franklin

Re: Search a match to a regex from the END of a string
by sundialsvc4 (Monsignor) on Mar 16, 2013 at 02:46 UTC

    An example of the data would help.   You can, of course, specify the “end-of-string anchor” character as part of your pattern.   Then, beyond that, consider whether the pattern should or should not be “greedy.”   (A greedy pattern always seeks the longest possible matching string.)

    It would be a good idea to test your regex against a number of representative strings that you do and that you don’t expect should match, e.g. with Test::More, to make very sure that the regex you come up with does what you need it to.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1023659]
Approved by marto
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (14)
As of 2014-07-31 16:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (249 votes), past polls