0

I want to use Regex.replace(input As String, pattern As String, replacement As String) in VB.net. The replacement string can contain substitutions of the form $1 or ${test}.

I need to call this with replacements I have no control over. So I'd like to escape all substitutions in my replacement strings. Java has Matcher.quoteReplacement() to do exactly that job.

  • Is there a Regex.quoteReplacement(replacement As String) As String or similar that escapes all substitutions in the given string?
  • Can I turn substitutions off?
  • Is there some alternative I could use instead?

Patrick Böker
  • 3,173
  • 1
  • 18
  • 24

1 Answers1

0

A possible solution that works in simple cases is:

Function quoteReplacement(text As String) As String
    Return Regex.Replace(text, "\$", "$$$$")
End Function

But I'm not at all sure that works in all corner cases there might be.

Patrick Böker
  • 3,173
  • 1
  • 18
  • 24