in reply to Re: getting answer 70 years off
in thread getting answer 70 years off

Oh, I can't believe I forgot to put the '-' at the end of the group, thanks for that. As for not needing to escape the '/', I'm afraid you're mistaken, at least for perl 5.14.2. I just tested it, and it doesn't compile: "Unmatched [ in regex;", and vim's highlighting ends the regex there, which in my opinion is reason enough to include it, even if it is unnecessary and benign. I did intend for that group to match one or more times, as some people may put spaces around their '\/-' ( sounds stupid, but there's a lot of stupid out there, and I try to account for all of it that I can ), but thanks for pointing it out.

Replies are listed 'Best First'.
Re^3: getting answer 70 years off
by kcott (Archbishop) on Jan 22, 2014 at 08:22 UTC
    "As for not needing to escape the '/', I'm afraid you're mistaken, at least for perl 5.14.2. I just tested it, and it doesn't compile: "Unmatched [ in regex;", ..."

    I'm currently using 5.18.1 but also have 5.14.2 available: I can't reproduce the error you report:

    $ perlbrew switch perl-5.14.2 $ perl -v This is perl 5, version 14, subversion 2 (v5.14.2) ... $ perl -Mstrict -Mwarnings -e 'my $re = qr{[ /\\-]}' $ perlbrew switch perl-5.18.1t $ perl -v This is perl 5, version 18, subversion 1 (v5.18.1) ... $ perl -Mstrict -Mwarnings -e 'my $re = qr{[ /\\-]}'

    The perlrecharclass link I provided was for 5.18.0. The perlrecharclass documentation for 5.14.2 has the same information.

    "... and vim's highlighting ends the regex there, which in my opinion is reason enough to include it, even if it is unnecessary and benign."

    I also use vim with syntax highlighting. I find it has many problems, particularly with regular expressions. I don't change my code to fit in with vim's problems.

    -- Ken

      Ah, if you write your regexes that way ( "qr{...}" ), then you wouldn't need to escape them, but when you use " /.../ " notation, they do have to be escaped, or else they end the regex. TMTOWTDI. Vim highlights both of these cases accurately in my limited testing.

      $ perl -Mstrict -Mwarnings -e 'my $re = /[ /\\-]/' Unmatched [ in regex; marked by <-- HERE in m/[ <-- HERE / at -e line + 1.

        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