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

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

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

    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.

        My whole point was that something like [a-] looks like it is a typo on the programmer's part. If I saw this in a script my first question would be "Did you mean something like [a-z]?" If I saw [-a] it doesn't look like a typo. Nor does [a\-] look like a typo. I can't really explain it any more clearly than this.

        And I should note that this is a lot more trouble than I thought it would be. ;) Such a simple question shouldn't ever have to go to a Re^7...