in reply to Regex to extract number from text file

Hi,

'\d' is used to match the digits in regular expressions. You can write the regular expression like

while (<FH>) { print "Matched Number:",$1 if(/(\d+)/g); }


you will get the matched number in $1.

Replies are listed 'Best First'.
Re^2: Regex to extract number from text file
by GrandFather (Saint) on Feb 18, 2009 at 20:32 UTC

    except that you don't want the /g (global) switch on the regex! Consider:

    use strict; use warnings; my $str = 'wibble 10'; print "Matched $1\n" if $str =~ /(\d+)/g; print "Matched $1\n" if $str =~ /(\w+)/g;

    Prints:

    Matched 10

    Because of the /g switch the second match fails because is starts searching from where the first match left off.


    True laziness is hard work