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

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.

Replies are listed 'Best First'.
Re^5: Checking input
by diotalevi (Canon) on Feb 25, 2003 at 21:55 UTC

    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.