in reply to Regex: find all trailing numerical digits in a string

As dragonchild points out above, if all you care about is

a final series of numbers
then all you need is /\d+$/. Add parentheses to capture that, and you have the answer above. Will that not work?

Replies are listed 'Best First'.
Re^2: Regex: find all trailing numerical digits in a string
by Fletch (Bishop) on Dec 06, 2004 at 18:09 UTC

    Well you really don't even need parens if you can live with $& or substr( $str, $-[0], $+[0] - $-[0] ) . . . </nit> :)

      It would seem to me that $& would be more readable than substr( $str, $-[0], $+[0] - $-[0] ).

      use strict; while(<DATA>) { chomp; /\d+$/; print "Numeric: [$&]\n"; } __DATA__ asdf_sadf-3i897sdyf33987 3w45t-wevt453_838475693

      Is there a case where you would use the substr over $&?


      "Look, Shiny Things!" is not a better business strategy than compatibility and reuse.


      OSUnderdog
        $& slows down every regex in the entire application. Generally, you shouldn't use it unless you literally have no other way of doing things.

        Being right, does not endow the right to be rude; politeness costs nothing.
        Being unknowing, is not the same as being stupid.
        Expressing a contrary opinion, whether to the individual or the group, is more often a sign of deeper thought than of cantankerous belligerence.
        Do not mistake your goals as the only goals; your opinion as the only opinion; your confidence as correctness. Saying you know better is not the same as explaining you know better.