in reply to Re^4: getting answer 70 years off
in thread getting answer 70 years off
Yes, you're quite correct: when using '/' as a delimiter, you'll need to escape instances of that delimiter in the regex. I should've looked more closely at the delimiters you were using — my apologies.
However, this is true for any delimiter. Here I needed to escape the '-' but not the '/' (with m-...-); with a delimiter not occurring in the regex (e.g. m!...!), no special escapes are required:
$ perl -Mstrict -Mwarnings -le 'my $x = "-"; print $x =~ m-[ /\\-]- ? +1 : 0' Unmatched [ in regex; marked by <-- HERE in m/[ <-- HERE /\\/ at -e l +ine 1. $ perl -Mstrict -Mwarnings -le 'my $x = "-"; print $x =~ m-[ /\\\-]- ? + 1 : 0' 1 $ perl -Mstrict -Mwarnings -le 'my $x = "-"; print $x =~ m![ /\\-]! ? +1 : 0' 1
You may be interested in this (from "perlop: Regexp Quote-Like Operators"):
"If "/" is the delimiter then the initial m is optional. With the m you can use any pair of non-whitespace (ASCII) characters as delimiters. This is particularly useful for matching path names that contain "/", to avoid LTS (leaning toothpick syndrome)." [my emphasis]
-- Ken
|
|---|