[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [MacPerl] How to match \* (MacPerl bug or my mistake)?



remove-thiis-freytag@remove.thiis.freytag.org.ethz.ch writes:

}I have been digging through the O'Reilly book 
}_Learning_Perl_.  Ran into a problem and I can't 
}believe that MacPerl isn't doing it right.  
}
}The goal is to match 0 or more / marks.  I can't
}do it and I -think- I'm coding correctly.  
}
}Here is the test script, run it and it explains the 
}problems I'm seeing:
}
}#!/usr/bin/perl -w
}
}print "\n\n\n\nBegin test taken from answer of problem 7.1.b in
}_Learning_Perl_, Schwartz and Randall (O'Reilly)";
}print "\nHere are various attempts to match and substitute for the
}string \\\\\\***** in the string aaaaa\\\\\\*****bbbbb.";
}
}$a = "aaaaa\\\\\\*****bbbbb";
}print "\n\n\$a:$a";
}$a =~ m#(\\*\**)#;

Old shell programmer's lore:

Try $a =~ m#(\\\\*\**)#

Notice that you had to do just this below to get what you wanted printed out.  I'm not exactly sure why you need to do this in the regexp, but I'm sure the Camel will enlighten.

}print "\n\$a:$a is matching with m#(\\\\*\\**)#";
}print "\n\$a should be:aaaaa\\\\\\*****bbbbb and that is exactly what I
}got";
}print "\n\$1:$1";
}print "\n\$1 should be:\\\\\\****** but I got nothing.";
}
}
}$a = "aaaaa\\\\\\*****bbbbb";
}print "\n\n\$a:$a";
}$a =~ s#(\\*\**)#c#;

Ditto here:

$a =~ s#(\\\\*\**)#c#;

}print "\n\$a:$a is substituting with s#(\\\\*\\**)#c#";
}print "\n\$a should be:aaaaacbbbbb but I got caaaaa\\\\\\*****bbbbb";
}print "\n\$1:$1";
}print "\n\$1 should be:\\\\\\****** but I got nothing.";
}
}$a = "aaaaa\\\\\\*****bbbbb";
}print "\n\n\$a:$a";
}$a =~ s#(b*)#c#;

Here Perl does what you ask it to.  Your expectations are just wrong.  You're asking it to match 0 or more b's, and the first place it matches is the null at the beginning of the string (which is 0 b's).  So it puts a c in place of the null (just what you asked it), and sets $1 = "".

}print "\n\$a:$a is substituting with s#(b*)#c#";
}print "\n\$a should be:aaaaa\\\\\\*****c but I got
}caaaaa\\\\\\*****bbbbb";
}print "\n\$1:$1";
}print "\n\$1 should be:\\\\\\****** but I got nothing.";
}
}$a = "aaaaa\\\\\\*****bbbbb";
}print "\n\$a:$a";
}$a =~ s#(b+)#c#;
}print "\n\$a:$a is substituting with s#(b+)#c#";
}print "\n\$a should be:aaaaa\\\\\\*****c and that is exactly what I
}got";
}print "\n\$1:$1";
}print "\n\$1 should be:bbbbb and that is exactly what I got";
}print "\n\nSo it looks like I am not getting greedy match for b* and
}that \\\\* does not match at all.";
}print "\nLooks like a bug in my version of MacPerl (v5.1.3r2), which I
}am running on a Quadra 700,";

It's very unlikely that Matthias would have changed the regexp engine, so you shouldn't leap to the conclusion that MacPerl has a bug.  Try this some time on a Unix machine (I did), and you'll get the same answer.  It seems in this case the book is wrong, and maybe it's wrong because something changed between Perl 4 and Perl 5.

}print "\nMacOS 7.5.5 with 20 Mb Physical RAM, 30 Mb total with Virtual
}Memory running.";
}
}print "\n\nPlease let me know what I am doing wrong or that I'm not if
}that is the case; thanks.";
}
}***** Want to unsubscribe from this list?
}***** Send mail with body "unsubscribe" to mac-perl-request@iis.ee.ethz.ch


---
Paul J. Schinder
NASA Goddard Space Flight Center
Code 693, Greenbelt, MD 20771
schinder@pjstoaster.pg.md.us



***** Want to unsubscribe from this list?
***** Send mail with body "unsubscribe" to mac-perl-request@iis.ee.ethz.ch