Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2011-09-26
5
Medium Priority
?
278 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
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…

705 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