Regular Expressions

Hi

How can replace all text in tags that starts with

 <td background="Images/top.gif">

and parse the content of (my_own_string_text) variable

String my_own_string_text ='test.gif';

<td background='test.gif'>

vbturbo
LVL 18
vbturboAsked:
Who is Participating?
 
Ravi SinghConnect With a Mentor Senior Software EngineerCommented:
Hi, use the following, the regex should now match single and double quotes in the input:

...
inputData = Regex.Replace(inputData, "(<td.*?background=)(\"|').+?(\"|')", "$1" + my_own_string_text);

The replacement string just needs to be surrounded with whatever quotes you want to use, for single quotes use:

string my_own_string_text = "'test.gif'";

Hope this helps!
0
 
Ravi SinghSenior Software EngineerCommented:
Hi, try:

using System.Text.RegularExpressions;

...

string inputData = "your html data here";
string my_own_string_text = "\"test.gif\"";

inputData = Regex.Replace(inputData, "(<td background=)\".+?\"([^>]*>)", "$1" + my_own_string_text + "$2");


0
 
Ravi SinghSenior Software EngineerCommented:
This one would be better

inputData = Regex.Replace(inputData, "(<td.*?background=)\".+?\"", "$1" + my_own_string_text);
0
 
vbturboAuthor Commented:
Thanks Zephyr

one final question

if i want produce/ and parse single quotes instead of double quotes

like this

'test.gif'

instead of

"\"test.gif\""

0
 
vbturboAuthor Commented:
Thanks a lot
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.