Re: Pattern mattching
by monkfan (Curate) on May 30, 2005 at 07:16 UTC
|
$ perl -e '$str = "+000000"; $match = $str =~ /^\+\d{6}/;
print "MATCH\n" if($match);'
prints:
MATCH
| [reply] [d/l] [select] |
|
It fails for $str ='+0000001';
| [reply] |
|
But you said it is fixed to "six digits". This should work, for any number of digits:
$match = $str =~ /^\+\d+/;
| [reply] [d/l] |
|
|
|
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.
| [reply] [d/l] |
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.) | [reply] [d/l] [select] |
|
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.)
| [reply] |
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.
| [reply] [d/l] [select] |
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}/;.
| [reply] |
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;
| [reply] [d/l] [select] |
Re: Pattern mattching
by gopalr (Priest) on May 30, 2005 at 07:19 UTC
|
$a=~m#\+\d{6}#
| [reply] [d/l] |
Re: Pattern mattching
by sh1tn (Priest) on May 30, 2005 at 07:22 UTC
|
my $pattern = qr{\+(?=\d{6})};
$string =~ /$pattern/;
| [reply] [d/l] |
Re: Pattern mattching
by gube (Parson) on May 30, 2005 at 07:22 UTC
|
$a = "+000000";
print "correct" if ($a =~ m#\+\d{6}#gsi)
| [reply] [d/l] |
Re: Pattern mattching
by Anonymous Monk on May 30, 2005 at 08:03 UTC
|
Thanks Everyone for the solution. | [reply] |
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. | [reply] [d/l] [select] |