in reply to Re: Replacing left angle bracket with HTML entity when between two backtick characters
in thread Replacing left angle bracket with HTML entity when between two backtick characters

If the backslash is allowed to escape backslashes, your approach of using a lookbehind will fail for \\`. I think you could fix that by looking for an odd number of preceding backslashes, but I prefer to look forward only instead.

[ 'is \\\\`my <this> that `but <this> one` no', 'is \\\\`my &lt;this> that `but <this> one` no', ],
  • Comment on Re^2: Replacing left angle bracket with HTML entity when between two backtick characters
  • Select or Download Code

Replies are listed 'Best First'.
Re^3: Replacing left angle bracket with HTML entity when between two backtick characters
by AnomalousMonk (Archbishop) on Sep 20, 2018 at 07:43 UTC

    This deals with the specific case you present, but I don't think I'm really dealing in a robust way with escaped backticks and escaped escapes. (Aiieee...) And I don't really like capturing stuff then stuffing it back in a replacement. Oh, well... I gotta go to bed now.

    c:\@Work\Perl\monks>perl -wMstrict -le "use 5.010; ;; use warnings; use strict; ;; use Test::More 'no_plan'; use Test::NoWarnings; ;; my @tests = ( [ 'is `my <string>` that `also <this> one` too', 'is `my &lgt;string>` that `also &lgt;this> one` too', ], [ 'is `not <this> one', 'is `not <this> one', ], [ 'is \`my <NO> that `but <this> one` yes', 'is \`my <NO> that `but &lgt;this> one` yes', ], [ 'is \\\\`my <this> that `but <this> one` no', 'is \\\\`my &lgt;this> that `but <this> one` no', ], ); ;; VECTOR: for my $ar_vector (@tests) { if (not ref $ar_vector) { note $ar_vector; next VECTOR; } ;; my ($string, $expected) = @$ar_vector; ;; (my $got = $string) =~ s{ (?<! (?<! \\) \\) ` [^`]* \K < ([^`]* `) } {&lgt;$1}xmsg; ;; is $got, $expected, qq{'$string' -> '$expected'}; } ;; done_testing; " ok 1 - 'is `my <string>` that `also <this> one` too' -> 'is `my &lgt;s +tring>` that `also &lgt;this> one` too' ok 2 - 'is `not <this> one' -> 'is `not <this> one' ok 3 - 'is \`my <NO> that `but <this> one` yes' -> 'is \`my <NO> that +`but &lgt;this> one` yes' ok 4 - 'is \\`my <this> that `but <this> one` no' -> 'is \\`my &lgt;th +is> that `but <this> one` no' 1..4 ok 5 - no warnings 1..5


    Give a man a fish:  <%-{-{-{-<