0

I want to find all HTML tags from the input strings and removed/replace with some text. suppose that I have string
INPUT=>

<img align="right" src="http://www.groupon.com/images/site_images/0623/2541/Ten-Restaurant-Group_IL-Giardino-Ristorante2.jpg" /><p>Although Italians originally invented pasta as a fastener to keep Sicily from floating away, <a href="http://www.tenrestaurantgroup.com/">Il Giardino Ristorante</a> in Newport Beach.</p>

OUTPUT=>

string strSrc="http://www.groupon.com/images/site_images/0623/2541/Ten-Restaurant-Group_IL-Giardino-Ristorante2.jpg";

<p>Although Italians originally invented pasta as a fastener to keep Sicily from floating away, http://www.tenrestaurantgroup.com in Newport Beach.</p>

From above string
if <IMG> tag found then I want to get SRC of the tag,
if <A> tag found then I want get HREF from the tag. and all other tag as same it is..

How can I achieved using Regex in C#.net?

3
  • 1
    Mandatory reading: RegEx match open tags except XHTML self-contained tags Commented Mar 2, 2011 at 8:54
  • This is a really simple regex. What do you need it for? Commented Mar 9, 2011 at 2:18
  • @sln I used HtmlAgilityPack for solve above problem. Commented Mar 9, 2011 at 5:50

3 Answers 3

1

You really, really shouldn't use regex for this. In fact, parsing HTML cannot be done perfectly with regex. Have you considered using an XML parser or HTML DOM library?

Sign up to request clarification or add additional context in comments.

Comments

0

You can use HtmlAgilityPack for parsing (valid/non valid) html and get what you want.

Comments

0

I agree with Justin, Regex really isn't the best way to do this, and the HTML Agility is well worth a look if this is something you will need to be doing alot of.

With that said, the expression below will store attributes into a group from where you should be able to pull them into your text while ignoring the rest of the element. :

</?([^ >]+)( [^=]+?="(.+?)")*>

Hope this helps.

Comments

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.