Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Regex Question in Perl

by mmittiga17 (Scribe)
on Dec 22, 2010 at 18:26 UTC ( #878625=perlquestion: print w/replies, xml ) Need Help??

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 ) {
      By the way, /[\d]+/ can be written /\d+/.

      ... and  /g still makes no sense.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://878625]
Approved by ikegami
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (4)
As of 2023-02-08 06:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I prefer not to run the latest version of Perl because:







    Results (40 votes). Check out past polls.

    Notices?