in reply to Regex extracting a number from numbers and text
$text =~ /\b(\d+)/
Update: By the way, you shouldn't use $1 unless you made sure the match succeeded.
if ($text =~ /\b(\d+)\b/) { print("Matched $1\n"); }
Update: Other solutions:
# The first standalone number. $text =~ /\b(\d+)\b/
# By context. $text =~ /(\d+) months? old/
# The last number. $text =~ /^.*\b(\d+)/
|
|---|