in reply to Remove non-numerics from string

if all your values are of the form XXX_DDDD.doc, then you can use
$oldValue =~ /_(\d+)\./; $newValue = $1;

Replies are listed 'Best First'.
Re: (2) Remove non-numerics from string
by ysth (Canon) on Jan 26, 2004 at 23:59 UTC
    It's a good idea to always check if a regex succeeds before using $1 and friends, or you may get a result from a previous match. One way to do this implicitly: ($newValue) = $oldValue =~ /_(\d+)\./; This leaves $newValue as undef on failure (because the match returns an empty list). You can even set newValue and check the result all at once: ($newValue) = $oldValue =~ /_(\d+)\./ or warn "bad format: '$oldValue'"; or
    if (($newValue) = $oldValue =~ /_(\d+)\./) { # all is ok } else { # something went wrong }