Solved

Regular Expressions

Posted on 2006-11-01
5
206 Views
Last Modified: 2010-04-16
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
0
Comment
Question by:vbturbo
  • 3
  • 2
5 Comments
 
LVL 18

Expert Comment

by:Ravi Singh
ID: 17848904
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
 
LVL 18

Expert Comment

by:Ravi Singh
ID: 17848949
This one would be better

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

Author Comment

by:vbturbo
ID: 17849522
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
 
LVL 18

Accepted Solution

by:
Ravi Singh earned 500 total points
ID: 17849600
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
 
LVL 18

Author Comment

by:vbturbo
ID: 17849960
Thanks a lot
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

948 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now