137

My question is a simple one, and it is about regular expression escaping. Do you have to escape a forward slash / in a regular expression? And how would you go about doing it?

Gumbo
  • 643,351
  • 109
  • 780
  • 844
Zerobu
  • 2,561
  • 7
  • 25
  • 32
  • 1
    What language/regular expression implementation do you use? – Gumbo May 20 '11 at 18:40
  • 1
    Interestingly enough, I was looking for this question for Javascript. But then my IDE said I was using an unnecessary escape. So `myStr.replace(/[/:.-]+/gi, '_')` is valid to my surprise. I thought I was going to need `/[\/:.-]+/gi`. I can't decide if this is cool or confusing. – Turbo Feb 15 '19 at 01:02

6 Answers6

117

What context/language? Some languages use / as the pattern delimiter, so yes, you need to escape it, depending on which language/context. You escape it by putting a backward slash in front of it: \/ For some languages (like PHP) you can use other characters as the delimiter and therefore you don't need to escape it. But AFAIK in all languages, the only special significance the / has is it may be the designated pattern delimiter.

nhahtdh
  • 55,989
  • 15
  • 126
  • 162
CrayonViolent
  • 32,111
  • 5
  • 56
  • 79
42

Here are a few options:

  • In Perl, you can choose alternate delimiters. You're not confined to m//. You could choose another, such as m{}. Then escaping isn't necessary. As a matter of fact, Damian Conway in "Perl Best Practices" asserts that m{} is the only alternate delimiter that ought to be used, and this is reinforced by Perl::Critic (on CPAN). While you can get away with using a variety of alternate delimiter characters, // and {} seem to be the clearest to decipher later on. However, if either of those choices result in too much escaping, choose whichever one lends itself best to legibility. Common examples are m(...), m[...], and m!...!.

  • In cases where you either cannot or prefer not to use alternate delimiters, you can escape the forward slashes with a backslash: m/\/[^/]+$/ for example (using an alternate delimiter that could become m{/[^/]+$}, which may read more clearly). Escaping the slash with a backslash is common enough to have earned a name and a wikipedia page: Leaning Toothpick Syndrome. In regular expressions where there's just a single instance, escaping a slash might not rise to the level of being considered a hindrance to legibility, but if it starts to get out of hand, and if your language permits alternate delimiters as Perl does, that would be the preferred solution.

DavidO
  • 13,812
  • 3
  • 38
  • 66
  • 1
    Can you give an example? I have this: `perl -pi -e "s/chdir .*/chdir $ROBOT_PATH/g" startup_scripts/supervisord.conf` And I'm getting conflicts with forward slashes. – CMCDragonkai Nov 08 '13 at 23:37
  • Note that you use an `s`, not an `m`, when doing a replace (aka substitute) with regular expressions. http://www.perlfect.com/articles/regex.shtml – Mashmagar Nov 24 '14 at 16:21
  • 2
    @CMCDragonkai `perl -pi -e "s{chdir .*}{chdir $ROBOT_PATH}g" startup_scripts/supervisord.conf` ... but this is probably better: `perl -pi -e 's/chdir .*/chdir $ENV{ROBOT_PATH}/g' startup_scripts/supervisord.conf` because it avoids shell interpolation. – DavidO Sep 06 '15 at 16:44
  • 1
    An alternative to escaping the literal `/` character is to use regex functionality of specifying a character by its ASCII encoding, in hex or octal. Perl accepts the octal form `\57` (source https://www.regular-expressions.info/refcharacters.html) – lukeuser Jun 28 '18 at 02:28
  • In the page linked by lukeuser (thank you) there is also the Escape Sequence \Q...\E . This worked for me. – user3012857 Feb 19 '20 at 13:40
11

Use the backslash \ or choose a different delimiter, ie m#.\d# instead of /.\d/ "In Perl, you can change the / regular expression delimiter to almost any other special character if you preceed it with the letter m (for match);"

4

If the delimiter is /, you will need to escape.

fvox
  • 1,077
  • 6
  • 8
2

If you are using C#, you do not need to escape it.

ShrapNull
  • 1,090
  • 1
  • 9
  • 15
1

For java, you don't need to.

eg: "^(.*)/\\*LOG:(\\d+)\\*/(.*)$" ==> ^(.*)/\*LOG:(\d+)\*/(.*)$

If you put \ in front of /. IDE will tell you "Redundant Character Escape "\/" in ReGex"

Dr.Bit
  • 719
  • 1
  • 6
  • 14