Solved

String.Replace using Regexp patterns id="....." to become String.Empty

Posted on 2011-09-26
5
270 Views
Last Modified: 2012-05-12
I have some code that scrapes HTML from other pages in my application.  The scraped HTML code looks like this (full of ASP.NET id values):

<h1 id="ctl00_TabPanelHolder1_pageHeader"><p><img id="ctl00_TabPanelHolder1_ContentPlaceHolderBody_Image7" src="images/reports.jpg" alt="" /></p>

Open in new window


Regular expressions aren't my speciality, so attempts have produced dismal results so far.  So, could anyone please provide me with the Regexp pattern to remove all id="....." strings so that the above code would become:

<h1><p><img src="images/reports.jpg" alt="" /></p>

Open in new window


Thanks in advance!
0
Comment
Question by:Rouchie
  • 2
  • 2
5 Comments
 
LVL 16

Expert Comment

by:disrupt
ID: 36599276
Try this regexp:

"id=[',\"]([\w- ])*[',\"]"
0
 
LVL 25

Author Comment

by:Rouchie
ID: 36599310
Thanks for that rapid reply!

That seems to work well, it just leaves a trailing space:

<h1 > 

Open in new window


Could you please explain how your pattern works, as it looks a lot more complex than the examples I was working from?  Thanks again.
0
 
LVL 16

Accepted Solution

by:
disrupt earned 500 total points
ID: 36599739
Sure the [] square brackets matches a single character out of all possibilities.
and as you can see matches either " or ' in this case [',\"]

the \w matches word characters, the - Specifies a range of characters.

* is used to repeat the condition

you can try something like so to remove white space beforehand

@"\s+id=[',\""]([\w-])*[',\""]"

0
 
LVL 25

Author Closing Comment

by:Rouchie
ID: 36599764
Absolutely superb.  Thank you very much.
0
 
LVL 74

Expert Comment

by:käµfm³d 👽
ID: 36599769
You may wish to make the quotation marks optional ( ? ) since not all HTML enforces quotation marks around attribute values  = )

I'd even go so far as to say you could simply use:

id=\S+

Open in new window


since whitespace isn't typically permitted in an ID attribute's value.
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

744 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

13 Experts available now in Live!

Get 1:1 Help Now