Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi fellow Monks,
I was wondering if it is possible to apply a pattern match that would look "backwards", i.e. instead of the typical
 if ($_=~/^(\d+)/)
I could start looking for numbers from the end of the string and "to the left".

Replies are listed 'Best First'.
Re: Pattern match from the end of string?
by toolic (Bishop) on Oct 19, 2016 at 13:03 UTC
      Great, many thanks to both !
Re: Pattern match from the end of string?
by BrowserUk (Patriarch) on Oct 19, 2016 at 13:02 UTC

    The simplest way is to reverse the string before you search: $_ = reverse;; then use the regex.

    Remember to scalar reverse the numbers you extract before using them.


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority". I knew I was on the right track :)
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re: Pattern match from the end of string?
by perldigious (Priest) on Oct 19, 2016 at 13:25 UTC

    Jeff Pinyan (aka Japhy) reversed regular expressions (aka sexeger)

    Update: Changed link to be internal to PerlMonks and direct from japhy.

    ReUpdate: Updated link to use only the internal node id instead as I was told the old one with the full address was logging people out of PerlMonks. My bad, didn't know it would do that.

    I love it when things get difficult; after all, difficult pays the mortgage. - Dr. Keith Whites
    I hate it when things get difficult, so I'll just sell my house and rent cheap instead. - perldigious