in reply to Re: Replace only unescaped metachars
in thread Replace only unescaped metachars

Close, but no cigar
my $string = '\\\\\\a'; # Unprocessed: \\\a # Should be: \a # Result: \\a
my $string = '\\\\\\?'; # Unprocessed: \\\? # Should be: \? # Result: \\#

See my top-level reply for a solution.

Replies are listed 'Best First'.
Re^3: Replace only unescaped metachars
by Anno (Deacon) on Feb 22, 2007 at 18:17 UTC
    Yes, I knew it had limitations which are hard to fix in an s/// approach.

    The recognition of escaped escapes is a naturally recursive problem. Regular expressions, even Perl's, are notoriously bad at that. Your remark about wanting a parser is spot on.

    Anno

      There's no recursion here (which is why the OP's parser is really just a tokeniser). The problem is capturing repeatedly, and knowing which rule (regexp piece) captured a given capture.
        The recursion I vaguely had in mind was
        my $escape; $escape = qr/(?<!(??{ $escape}))\\/;
        It fails, of course, because it would require variable-length lookbehind.

        Anno