in reply to Re: @ in regex, or not?
in thread @ in regex, or not?

Important correction: outside of a character class, . means "match anything" (except a newline, unless told otherwise), but inside a character class, it DOES mean a literal dot.

Philosophy can be made out of anything. Or less -- Jerry A. Fodor

Replies are listed 'Best First'.
Re^3: @ in regex, or not?
by tadman (Prior) on Mar 26, 2001 at 22:55 UTC
    Strange but true. It is odd, though, that '@' is parsed in there as meaning array, but the previously all-powerful '.' reverts back to meaning just a dot. Like Kryptonite does to Superman?
      More fun with Deparse and the all too fleeting concept of special characters, giving further evidence that our character set is perhaps just not rich enough for Perl's liking ;-):
      === 1 === perl -MO=Deparse -e "$_='abc@de';print if(/[_@]/)" $_ = 'abc@de'; print $_ if /[_\@]/; -e syntax OK perl -e "$_='abc@de';print if(/[_@]/)" abc@de === 2 === perl -MO=Deparse -e "$_='abc@de';print if(/[@_]/)" $_ = 'abc@de'; print $_ if /[@_]/; -e syntax OK perl -e "$_='abc@de';print if(/[@_]/)" /[]/: unmatched [] in regexp at -e line 1. === 3 === perl -MO=Deparse -e "$_='abc@de';print if(/[_@a]/)" In string, @a now must be written as \@a at -e line 1, near "[_@a" -e had compilation errors. $_ = 'abc@de'; print $_ if /[_@a]/; === 4 === perl -e "@a[0]='z';@a[23]='oob';$_='abc@de';print if(/[_@a]/)" abc@de
      My guess: In the first example, Perl has escaped the @ itself since it saw no array there, but in the second example, it saw an array, and accepted the syntax, but in evaluation it vanished. In the third case, Perl knows there is no @a array and barfs, telling be to be more specific, however if @a does exist (example 4), Perl proceeds, though perhaps not doing what the author intended - it concatenates all the elements of @a and matches against the resulting string (that I didn't expect).

      Usually when I play around like this I learn something interesting, but as often as not I also end up with new questions!

      --
      I'd like to be able to assign to an luser

        The regex parser has some rather convoluted heuristics that it follows to decide whether $ or @ is followed by a variable name. One side effect of this is that /[$]/ is an error.

        The only real documentation of this deep magic is in the source code (simply search for "weight" as it is only used on that code -- in toke.c, BTW).

                - tye (but my friends call me "Tye")