0

I have some code that uses double dollar signs to signify comment lines. I need to add some text prior to some of those comment lines. My goal is to search for those comments like "$$ Comment" and to replace with something like this, "Prior line" & vbNewLine & "$$ Comment". I am able to find it by escaping my search term "$$ Comment" with Regex.Escape() which modifies it to this; "\$\$\ Comment". But the replacement string that is produced when I pass this escaped string to Regex.Replace() is, due to the nature of the method, a single dollar; "$ Comment". Is there a work-around to the way that "$$" is always replaced as "$" when using Regex.Replace or String.Replace?

2
  • what language is this (other than regex)? Commented May 16, 2014 at 21:36
  • 1
    Please tag your question with the language (VB.Net?) and show the code you tried to write. Commented May 16, 2014 at 21:38

1 Answer 1

1

In the substitution string, you escape the dollar sign with another dollar sign: "$$$$ Comment". ref

Alternatively, you can capture the comment in a group (\$\$ Comment) and use a group reference in the substitution string to restore it: "$1"

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, just what I needed! Dim myRegex As New Regex(escapedSearchRegex) Dim Result As String = myRegex.Replace(RichTextBox3.Text, ReplaceText) 'fix the double dollar replacements Dim pattern As String = "^\$\ " Dim replacement As String = "$$$ " Dim Result2 As String = Regex.Replace(Result, pattern, replacement,RegexOptions.Multiline)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.