in reply to Re: Why \n matches but not $^? (weight)
in thread Why \n matches but not $^?

If you suspect that a regex isn't parsed the way you wanted it, you can use re 'debug'; to find out:
$ perl -Mre=debug -wle 'm/C$\n^D/' Freeing REx: `","' Omitting $` $& $' support. EXECUTING... Compiling REx `C n^D' size 6 Got 52 bytes for offset annotations. first at 1 rarest char D at 3 1: EXACT <C\nn>(3) 3: BOL(4) 4: EXACT <D>(6) 6: END(0) anchored "C ...
You don't have to understand everything to notice that the EXACT <C\nn> isn't what you were after. The literal <c>n has to come from the ...\n in the regex, so the thing before it (a newline) has to come from the previous token in the regex.

Replies are listed 'Best First'.
Re^3: Why \n matches but not $^? (dump)
by tye (Sage) on Oct 14, 2008 at 00:02 UTC

    Thanks. That reminded of one thing I had been trying to remember to include:

    $ perl -e'print qr/C$\n^D/,$/' (?-xism:Cn^D) $ perl -le'print qr/C$\n^D/' (?-xism:C n^D)

    which is a lower-tech way to notice that your regex wasn't parsed the way you expected. (It also demonstrates why I use -l: so I don't have a append newlines to each of my print statements.)

    - tye        

Re^3: Why \n matches but not $^? (weight)
by tel2 (Pilgrim) on Oct 13, 2008 at 22:53 UTC
    Thanks moritz,

    That's very helpful. I didn't know about that re debug module.

    BTW: Why are you and tye using Perl's -l switch?

    Terry

      BTW: Why are you and tye using Perl's -l switch?

      It adds a newline to every print statement. See perlrun for more information.

      If you have perl 5.10.0 or newer, you can say perl -wE 'say ...' instead.