2

I have inherited a Perl codebase that uses regex to parse an XML file. Not optimal, I know. I have lines of code like:

$title =~ s`<(.*?)>``g;

and

$content =~ s`__DOUBLEFIG__`${fig_html}`;

The standard Perl find and replace takes the form of

s/foo/bar/g;

What does

s`foo`bar`g;

do?

  • 2
    It's just a different delimiter. Using back ticks is dangerous IMHO as you can use them to fire off system commands. – squiguy May 05 '15 at 03:30

1 Answers1

4

Those are alternative ways of delimiting the regex. Whomever wrote that code chose to use something else for personal reasons or, possibly, for code readability. Often, if patterns are often going to involve /, something else will be chosen to avoid having to escape the slash character in the regex.

This answer provides more information.

Community
  • 1
  • 1
hrunting
  • 3,857
  • 25
  • 23
  • 2
    Good answer. I might be tempted to add that 'don't use backticks as a regex delimiter' because it's a rather confusing option. – Sobrique May 05 '15 at 10:46
  • Backticks are indeed unusual. Often I end up using a bracketed pair, such as s, because generally bracketed ones are easier to pair up when reading it. – LeoNerd May 06 '15 at 15:58
  • In the text editor I use (vim), its default syntax highlighting assumes either slash (`/`) or hash characters (`#`) for regular expression delimiters. – pcjr Apr 06 '17 at 16:38