So, I have a input string which has html styles data. The end goal is to locate the tag - "#TAG#0.00-0##0#[ ]". There could be multiple of these tags on the input string.
Once these tags are located, it needs to be replaced with static text with little tweak.
So, if the input string is -
"This is sample string #TAG#0.00-0##0#[ ] (a) this is another sample string. This is third sample string #TAG#0.00-0##0#[ This is a text] Another sample string."
The output string should look like -
"This is sample string [ ] (a) this is another sample string. This is third sample string [ This is a text] Another sample string "
How can this be done using regular expression or any other mechanism. Any hint would be highly appreciated.
VulpesPosted Sep 20, 2012, 11:24 AM
using System;
using System.Text.RegularExpressions;
class Program
{
static void Main()
{
string text = @"This is sample string #TAG#0.00-0##0#[ ] (a) this is another sample string. This is third sample string #TAG#0.00-0##0#[ This is a text] Another sample string.";
string regFind = @"(#TAG#0\.00-0##0#)(\[.*?])";
string regReplace = @"
text = Regex.Replace(text, regFind, regReplace);
Console.WriteLine(text);
Console.ReadKey();
}
}
The console output should be:
This is sample string
sample string. This is third sample string
s a text]
EDIT:
I've removed the preceding backslashes from the # characters in 'regFind' as it works fine without them. I thought from memory that # was a Regex meta-character but apparently it isn't.
Deer ParkPosted Sep 20, 2012, 1:35 PM
Appreciate your help. :)