Solved

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

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
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…

820 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