#!/usr/bin/perl # https://perlmonks.org/?node_id=1222687 use strict; use warnings; my $nobacktick = qr/(?:\\.|[^\\`]++)*+/s; while( ) { s/ $nobacktick \K ` $nobacktick ` / $& =~ s{<}{<}gr /gex; print; } __DATA__ This is `my ` that I want to modify because the angle bracket is between backticks This is `my <string>` that I want to modify because the angle bracket is between backticks This is `a I don't want to modify because it's not between backticks This is \`another `I don't want to modify because the first backtick is escaped but I do want to modify ` between the backticks `whole string with enclosed in backticks` `whole string with not enclosed in backticks `whole string with not enclosed in backticks\`