in reply to Re: Re: Re: Checking input
in thread Checking input

No - your usage is buggy. Consider perl -e 'print join "\n", qr/[\w\-]/,qr/[\w-]/,""'. Don't "escape" the hyphen because that's not what happens.


Seeking Green geeks in Minnesota

Replies are listed 'Best First'.
Re: Re: Re: Re: Re: Checking input
by Nkuvu (Priest) on Feb 25, 2003 at 20:55 UTC

    I'm not escaping the hyphen because it is a range, I am suggesting escaping it for a clearer regex. Or re-ordering the entries within the character class.

    Usage is buggy? How so? perl -e 'print join "\n", qr/[\w\-]/,qr/[\w-]/,""' prints

    (?-xism:[\w\-]) (?-xism:[\w-])
    on my system. That explains nothing to me.

      That demonstrates that the '\' character is not escaping anything and is infact, just being added to the character class as another potential match which is completely against the spirit of the code. In fact, writing the backslash in might lead some one (like you) to think that it was escaping the next thing when in this case it escaped nothing and is itself. If the slash worked like you expected it to both lines would read like (?:-xism:[\w-]).


      Seeking Green geeks in Minnesota

        I disagree. Consider the code:

        #!/usr/bin/perl $foo = "this is a \\ test"; print "Foo: ($foo)\n"; ($bar) = $foo =~ /([\-])/; print "Bar: ($bar)\n";
        By your reasoning $bar should be '\'. It isn't. This script prints
        Foo: (this is a \ test) Bar: ()

        Change $foo to "this is a \\ test - " and $bar becomes '-', not '\'. The \ before the - in the character class [\w\-] isn't escaping anything, true. But it doesn't get added to the character class.