Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Pattern mattching

by Anonymous Monk
on May 30, 2005 at 07:07 UTC ( #461664=perlquestion: print w/replies, xml ) Need Help??

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

Hello, I am new to perl and i am seeking to find an appropriate way to match a pattern exactly. The pattern that i need to match is '+000000'. I am not sure if $a =~ s/\+\d\d\d\d\d\d/ ; will do. I need to check for six (6) digits after the '+'. please advice.

Replies are listed 'Best First'.
Re: Pattern mattching
by monkfan (Curate) on May 30, 2005 at 07:16 UTC
    Hi,
    $ perl -e '$str = "+000000"; $match = $str =~ /^\+\d{6}/; print "MATCH\n" if($match);'
    prints:
    MATCH
    Regards,
    Edward
      It fails for $str ='+0000001';
        But you said it is fixed to "six digits". This should work, for any number of digits:
        $match = $str =~ /^\+\d+/;
        Regards,
        Edward
        The pattern +000000 is in that string. Did you want it to match only if the whole string fits the pattern? If so then /^\+\d{6}\z/ should work.
Re: Pattern mattching
by nobull (Friar) on May 30, 2005 at 07:29 UTC
    What do you mean by "exactly" exactly?

    Do you mean you only want a match if the pattern matches the whole string not just part of it?

    You do that by anchoring the pattern to the beginning and end /^\+\d\d\d\d\d\d$/.

    Note that the $ anchor actually permits a newline at the end of the string. If this bothers you then use \Z instead.

    As others have pointed out /^\+\d\d\d\d\d\d$/ is probably more tidy as /^\+\d{6}$/.

    (Oh, and BTW there was a spurious 's' in your OP - you should probably go update it.)

      Note that an Anonymous Monk cannot update his own posts. If it really bothers you, submit it to be edited. (I wouldn't suggest submitting it.)
Re: Pattern mattching
by pernod (Chaplain) on May 30, 2005 at 07:25 UTC

    You should be aware of the special meanings of $a and $b for sorting. Using these variables outside a sort block may lead to some surprises.

    You should also change s//; to m//;. Your snippet uses the substitution operator s, while you are after the match operator m. You should in any case take a look at the documentation that has been pointed to elsewhere in this thread.

    Good luck!

    pernod
    --
    Mischief. Mayhem. Soap.

Re: Pattern mattching
by Fang (Pilgrim) on May 30, 2005 at 07:19 UTC

    If you are "not sure if $a =~ s/\+\d\d\d\d\d\d/ ; will do", it means you have to read perlretut for a start, with possibly perlre as a followup.

    As for a direct answer, $a =~ m/\+\d{6}/;.

Re: Pattern mattching
by TedPride (Priest) on May 30, 2005 at 08:36 UTC
    my $c = '+000000'; print 'contains' if $c =~ /\+\d\d\d\d\d\d/;
    Note that the s flag is for substitution. What you want is a match, so the s is left out. Also, as people have stated above, $a is a reserved variable, and while you can use it for this, you probably shouldn't. Also, as stated above, your regex can be shortened:
    my $c = '+000000'; print 'contains' if $c =~ /\+\d{6}/;
    And if you need to match exactly:
    my $c = '+000000'; print 'contains' if $c =~ /^\+\d{6}$/; # or... print 'contains' if $c eq '+'.'0'x6;
Re: Pattern mattching
by gopalr (Priest) on May 30, 2005 at 07:19 UTC
Re: Pattern mattching
by sh1tn (Priest) on May 30, 2005 at 07:22 UTC
    Or positive lookahead:
    my $pattern = qr{\+(?=\d{6})}; $string =~ /$pattern/;


Re: Pattern mattching
by gube (Parson) on May 30, 2005 at 07:22 UTC
    $a = "+000000"; print "correct" if ($a =~ m#\+\d{6}#gsi)
Re: Pattern mattching
by Anonymous Monk on May 30, 2005 at 08:03 UTC
    Thanks Everyone for the solution.
Re: Pattern mattching
by DrHyde (Prior) on May 31, 2005 at 09:21 UTC
    Sure, I'll advice. I advice that you let people know what you've tried, how the results differed from what you expected, and how you tried to fix that. Only after that will I give you any more advise.

    I will, however, mention one thing which is not related to your problem but which you should be aware of. The $a and $b variables are special, and should not be used unless you really mean to use them. Which you don't. So don't.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (3)
As of 2023-12-10 05:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What's your preferred 'use VERSION' for new CPAN modules in 2023?











    Results (38 votes). Check out past polls.

    Notices?