Solved

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

Posted on 2011-09-26
5
272 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 75

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

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

Suggested Solutions

Title # Comments Views Activity
Visual Studio .Net 2012 and Crystal Reports 9 44
Error in page 3 44
Convert an ASPX page into PDF 7 21
Gridview alignment 1 0
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…

929 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

15 Experts available now in Live!

Get 1:1 Help Now