in reply to Re^3: Escape special characters for a LaTeX file
in thread Escape special characters for a LaTeX file

Sorry! One strange thing is happening: I get this:
1\0:\0\0
all "0" are escaped.

  • Comment on Re^4: Escape special characters for a LaTeX file

Replies are listed 'Best First'.
Re^5: Escape special characters for a LaTeX file
by AnomalousMonk (Archbishop) on Dec 14, 2014 at 16:28 UTC
    ... all "0" are escaped.

    That's because '0' (zero) is part of the character class: the value of  $% happens to be 0 at that point | the moment of regex compilation. Please see my update to the above.

    Update: E.g.:

    c:\@Work\Perl\monks\marek1703>perl -wMstrict -le "my $s = '-#-$-%-&-~-_-}-{-^-0-$%&~_}{^0'; print qq{'$s'}; ;; $s =~ s{ ([#\$%&~_{}^]) }{\\$1}xmsg; print qq{'$s'}; " '-#-$-%-&-~-_-}-{-^-0-$%&~_}{^0' '-\#-\$-\%-\&-\~-\_-\}-\{-\^-0-\$\%\&\~\_\}\{\^0'
    and also (no capture):
    c:\@Work\Perl\monks\marek1703>perl -wMstrict -le "my $s = '-#-$-%-&-~-_-}-{-^-0-$%&~_}{^0'; print qq{'$s'}; ;; $s =~ s{ (?= [#\$%&~_{}^]) }{\\}xmsg; print qq{'$s'}; " '-#-$-%-&-~-_-}-{-^-0-$%&~_}{^0' '-\#-\$-\%-\&-\~-\_-\}-\{-\^-0-\$\%\&\~\_\}\{\^0'