Escaping $_ in Regex.Replace

  • Thread starter Thread starter Theo Chakkapark
  • Start date Start date
T

Theo Chakkapark

I'm having issues trying to replace text with PHP.

For example, if I have a string of text that reads:

{tag}

And want to replace that with:

$_POST["var"]

I get the following result:

{tag}POST["var"]

The $_ seems to be replaced with the original text. The problem is, I
cannot seem to escape $_ at the same time. The text is stored in an
array. For example, I have two arrays, one with the text to find,
another with the text to replace. The arrays are loaded dynamically
from a text file that defines these find/replace tags. The following
did not work:

Regex.Replace(text, Regex.Escape(@arrlstModuleFind.ToString()),
Regex.Escape(@arrlstModuleReplace.ToString()),
RegexOptions.IgnoreCase);

Also, adding slashes to the replacement text does not work either,
such as \$_.

Any tips?
 
I'm not 100% sure if I got your question: Are you trying to escape the '$'
character in a regex substitution string? Have you tried "$$"?

These are the escaping rules for substitutions: http://tinyurl.com/6fbea

Niki
 
Back
Top