Acorn Arcade forums: Programming: Perl pattern matching question
|
Perl pattern matching question |
|
ksattic (22:35 29/7/2003) Matthew (23:27 29/7/2003) ksattic (15:15 30/7/2003)
|
|
Simon Wilson |
Message #44965, posted by ksattic at 22:35, 29/7/2003 |
Finally, an avatar!
Posts: 1291
|
Been mucking around with Perl today and I came across a pattern that I think should match a string, but doesn't.
Try the following:
#!/usr/bin/perl
$match = " D";
if($match =~ /^[\t| {8}]([A-Z|a-z|0-9]+).*/) { print "Matched\n"; }
With Perl 5.005 (on RISC OS) and 5.6.1 (on a Sun box), the line does not match. Funnily enough, the following does match:
#!/usr/bin/perl
$match = " D";
if($match =~ /^[\t| {8}]/) { print "Matched\n"; }
Can anyone please explain why the first does not match? Why does changing the eight spaces in the $match string to a tab cause it to match fine? |
|
[ Log in to reply ] |
|
Matthew Somerville |
Message #44967, posted by Matthew at 23:27, 29/7/2003, in reply to message #44965 |
Posts: 520
|
Can anyone please explain why the first does not match? You're using classes - [ and ] - where you mean to use normal brackets - ( and ). Your first line says "Match *one* of (tab, vertical bar, space, brace, or the digit 8) at the start of a string, followed by one or more of A-Z, a-z, 0-9 or vertical bar." So eight spaces followed by a D will not match, whereas a single tab followed by a D will. Changing the first set of square brackets to normal brackets will mean it does as you want.
Note you do not need to use | inside square brackets - you can simply say [A-Za-z0-9] - and that nearly everything inside [] is taken literally. |
|
[ Log in to reply ] |
|
Simon Wilson |
Message #44980, posted by ksattic at 15:15, 30/7/2003, in reply to message #44967 |
Finally, an avatar!
Posts: 1291
|
You're using classes - [ and ] - where you mean to use normal brackets - ( and ) Thanks for the help! I'd got confused somewhere along the way and though that [ and ] were the right way to go. |
|
[ Log in to reply ] |
|
|
Acorn Arcade forums: Programming: Perl pattern matching question |