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

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 gopalr (Priest) on May 30, 2005 at 07:19 UTC
```\$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 sh1tn (Priest) on May 30, 2005 at 07:22 UTC
```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)