Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: Replacing left angle bracket with HTML entity when between two backtick characters

by AnomalousMonk (Archbishop)
on Sep 20, 2018 at 06:55 UTC ( [id://1222695]=note: print w/replies, xml ) Need Help??


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

Here's a possible start on a pure-regex approach (needs Perl version 5.10+ for  \K regex operator):

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', ], ); ;; 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;}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' 1..3 ok 4 - no warnings 1..4
Add more test cases to refine. (In general, see How to ask better questions using Test::More and sample data and Short, Self-Contained, Correct Example.)


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

  • Comment on Re: Replacing left angle bracket with HTML entity when between two backtick characters
  • Select or Download Code

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

    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', ],

      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:  <%-{-{-{-<

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1222695]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (3)
As of 2024-04-25 05:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found