1
votes

I need a regex that can match on forward slashes and back slashes so that I can escape them. However I don't want the regex to match on the following string:

\\\\"Hello World\\\\"

So for example

 / / ////////" \\\\\\\   \\\\"Hello World\\\\"

It should match on each slash EXCEPT where there are four slashes followed by a quote with any characters followed by another four quotes with a quote.

1
Which programming language are you using ? Why do you need this? - Pedro Lobito
C# .NET I need to escape slashes before they are sent to elasticsearch for example BP 120/30 I would like it to be escaped 120//30 - Imran Azad
"BP 120/30 I would like it to be escaped 120//30" you're not escaping anything here. - Pedro Lobito
@PedroLobito sorry it was a typo, 120\\/30 - Imran Azad

1 Answers

1
votes

BP 120/30 I would like it to be escaped 120\\/30

Regex.Replace(subjectString, "(/{1})", @"\\$1");

Backslash example (BP 120\30):

Regex.Replace(subjectString, @"(\\{1})", @"\\\\$1");