$1 in reg ex

What does this mean

$var =~ s/\D*(\d+)\D*/$1/;
castelloAsked:
Who is Participating?
 
bebonhamConnect With a Mentor Commented:
it means

dasljda123123lkajslsjad  would become

123123

it looks for a string that might have some stuff at the begenning or at the end or both and definitely has to have at least 1 number in it, and then it removes everything except for the number. and then the $var is just the number

0
 
bebonhamCommented:
$1 is a reference to whatever is in side the first set of ()
0
 
iddo_levinCommented:
it's true that $1 is the string matched in the first use of the braces, but when using this within the regexp it's more elegent to use \1 instead.
0
 
ozoCommented:
Since it's not used within the regexp here, $1 is correct in the substitution
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.