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?
-
what language is this (other than regex)?Joeytje50– Joeytje502014-05-16 21:36:12 +00:00Commented May 16, 2014 at 21:36
-
1Please tag your question with the language (VB.Net?) and show the code you tried to write.Barmar– Barmar2014-05-16 21:38:42 +00:00Commented May 16, 2014 at 21:38
Add a comment
|
1 Answer
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"
1 Comment
user2904160
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)