0

Below is my code

Dim ATT As String = "Hi Sample $1$ Text"
Dim newText As String = "<ss>Hello How Are you</ss>"
newText = Regex.Replace(newText, "<ss>(.*?)</ss>", ATT, RegexOptions.IgnoreCase)

The above Code Treats the string content ("$1$") in ATT variable as Regex value and replaces it in the newText variable, how to overcome this, please guide,

Is there any thing available like we have in perl mentioned below to overcome this problem,

$_=~ s/<ss>(.*?)</ss>/\Q$ATT\E/g;
1
  • This is vast Coding, with lots of Variables like I mentioned, Is there a way to do it globally, like I posted the code in Perl?? Commented Mar 5, 2015 at 9:57

1 Answer 1

2

You can either escape the $ characters in the replacement string:

Dim ATT As String = "Hi Sample $1$ Text"
ATT = ATT.Replace("$", "$$")
Dim newText As String = "<ss>Hello How Are you</ss>"
newText = Regex.Replace(newText, "<ss>(.*?)</ss>", ATT, RegexOptions.IgnoreCase)

Or use a lambda for the replacement:

Dim ATT As String = "Hi Sample $1$ Text"
Dim newText As String = "<ss>Hello How Are you</ss>"
newText = Regex.Replace(newText, "<ss>(.*?)</ss>", Function(m) ATT, RegexOptions.IgnoreCase)
Sign up to request clarification or add additional context in comments.

4 Comments

where is the Function (M) :-)
That's a lambda... Maybe I got the syntax wrong, I'm not a VB user ;)
This is not the solution I am expecting, I already know this to replace $ and use it, but i have 100's of variables like ATT, can't replace and revert for all those things
So don't change ATT, do it like this: Regex.Replace(newText, "<ss>(.*?)</ss>", ATT.Replace("$", "$$"), RegexOptions.IgnoreCase)

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.