Solved

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

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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
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…

752 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