#perl use strict; use warnings; sub escape { my( $str ) = @_; $str =~ s!) { s!(?:^|\G)([^`\\]* # plain string without escape or backtick |\\. # anything, escaped )* (` # backtick (?:[^\\`]*|\\.) # only escaped backticks `) # closing backtick !$1.escape($2)!sgex; 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