in reply to Escaping Regex Expressions
If what you mean by "doesn't match" is "spews lots of errors", read on...
use strict; use warnings; $_ = 'Page 1 of 5'; if(Page\ \;1\ \;of\ \;(/d+)) { print "Number of Pages = ".$1; } __OUTPUT__ Backslash found where operator expected at test.pl line 9, near "Page\ +" Backslash found where operator expected at test.pl line 9, near "  +\" (Missing operator before \?) Backslash found where operator expected at test.pl line 9, near "1\" (Missing operator before \?) Backslash found where operator expected at test.pl line 9, near "  +\" (Missing operator before \?) Backslash found where operator expected at test.pl line 9, near "of\" Backslash found where operator expected at test.pl line 9, near "  +\" (Missing operator before \?) syntax error at test.pl line 9, near "Page\" Search pattern not terminated at test.pl line 9.
Ok, let's solve this one step at a time. First, the regexp operator is m//, which can be abbreviated as // most of the time. I don't see any regexp operator in your code. So we'll correct that part...
use strict; use warnings; $_ = 'Page 1 of 5'; if(/Page\ \;1\ \;of\ \;(/d+)/) { print "Number of Pages = ".$1; } __OUTPUT__ Unmatched ( in regex; marked by <-- HERE in m/Page 1 of  +;( <-- HER E / at test.pl line 9.
Hmmm, what's this unmatched ( in regexp business? Oh, I see. You've got (/d+)/. The regexp thinks that the '/' in /d+ is the end of the regexp. You probably really meant the \d+ metacharacter and quantifier. So we'll fix that...
use strict; use warnings; $_ = 'Page 1 of 5'; if(/Page\ \;1\ \;of\ \;(\d+)/) { print "Number of Pages = ".$1; } __OUTPUT__ Number of Pages = 5
Viola, it works!
Of course this makes the assumption that you're testing your regexp against a string held in $_. If instead you're testing against a string held in some other scalar variable, such as $string, you'll need to use the binding operator also. The binding operator is '=~', and is used like this:
$string =~ m/regexp goes here/
See perlretut and perlrequick for an introduction to Perl's regular expressions. For additional reading, you can dive into perlre and perlop.
Update: I see I've wasted my time, because your original question wasn't really the question you wanted to ask. It is foolish to retype your code when inserting it here. Cut and paste it, or boil it down to a tiny script that replicates the behavior and cut and paste that. Retyping it obviously introduced numerous other errors and led us down the wrong path toward correcting them. Your real problem, assuming you've now typed it correctly, is probably that your input text is not what you think it is.
Dave
|
|---|