http://qs1969.pair.com?node_id=878625

mmittiga17 has asked for the wisdom of the Perl Monks concerning the following question:

Hi All,

I am trying to get the number into $acct that follows "Account number" in a string. This should work but it is not, what am I doing wrong? Thanks!

$page2 = "Account number 12345678"; if ( $page2 =~ m/Account number (.*?)/g ) { $acct = $1; print "acct = $acct\n"; }

Replies are listed 'Best First'.
Re: Regex Question in Perl
by ikegami (Patriarch) on Dec 22, 2010 at 18:29 UTC

    The "?" in ".*?" makes ".*" matches as few characters as possible. Get rid of it.

    Furthermore, the "g" in if (/.../g) makes no sense conceptually (check if there's a match, then check again to be sure?) or in practice (it has weird side effects). Get rid of it.

    I dislike using global variables for no reason, so I'd write it

    my $page2 = "Account number 12345678"; if ( my ($acct) = $page2 =~ /Account number (.*)/ ) { print "acct = $acct\n"; }
Re: Regex Question in Perl
by toolic (Bishop) on Dec 22, 2010 at 18:34 UTC
    Tip #9 from the Basic debugging checklist:
    use warnings; use strict; use YAPE::Regex::Explain; print YAPE::Regex::Explain->new('Account number (.*?)')->explain(); __END__ The regular expression: (?-imsx:Account number (.*?)) matches as follows: NODE EXPLANATION ---------------------------------------------------------------------- (?-imsx: group, but do not capture (case-sensitive) (with ^ and $ matching normally) (with . not matching \n) (matching whitespace and # normally): ---------------------------------------------------------------------- Account number 'Account number ' ---------------------------------------------------------------------- ( group and capture to \1: ---------------------------------------------------------------------- .*? any character except \n (0 or more times (matching the least amount possible)) ---------------------------------------------------------------------- ) end of \1 ---------------------------------------------------------------------- ) end of grouping ----------------------------------------------------------------------

    Update: fixed typo (thanks Anomalousmonk)

Re: Regex Question in Perl
by AR (Friar) on Dec 22, 2010 at 18:33 UTC

    Hi mmittiga,

    You're using the lazy modifier to your .*. When the regex engine gets to this point, it doesn't attempt to match anything. If you know account numbers are all digits, try changing the line to:

    if ( $page2 =~ m/Account number ([\d]+)/g ) {

      ... and  /g still makes no sense.

      By the way, /[\d]+/ can be written /\d+/.