473,322 Members | 1,781 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

RegEx : Match and replace term within HTML tags

I have a search app that searches local HTML files for a specified
term. I then display the pages that contain the term.

I would like to highlight the search term within the HTML when it is
viewed.

I have the following regular expression code:

string searchTerm = "(?<STARTTAG>(<[^>]*>.*))(?<MATCHTERM>(" +
lastSearchTerm + "))(?<ENDTAG>(.*<[^>]*>))";

string replaceString = "${STARTTAG}<span
style=\"background-color:#FFFFCC\">${MATCHTERM}</span>${ENDTAG}";

Regex.Replace(htmlBody, searchTerm, replaceString,
RegexOptions.IgnoreCase);

I am trying to match the search term within HTML tags. i.e.

<htmltag>searchterm</htmltag>

and then replace the search term with a span tag to color it, like so:

<htmltag><span
style=\"background-color:#FFFFCC\">searchterm</span></htmltag>

This works, but works inconsitently (and without a discernable pattern
when it fails).

So, does anyone see anything obviously wrong with my Regular
Expressions? I am pretty new to regular expressions, although I
usually know enough to get stuff done.

mike c
Nov 16 '05 #1
1 3187
Hi,
inline

"mike c" <m@foo.com> wrote in message
news:r1********************************@4ax.com...
I have a search app that searches local HTML files for a specified
term. I then display the pages that contain the term.

I would like to highlight the search term within the HTML when it is
viewed.

I have the following regular expression code:

string searchTerm = "(?<STARTTAG>(<[^>]*>.*))(?<MATCHTERM>(" +
lastSearchTerm + "))(?<ENDTAG>(.*<[^>]*>))";

string replaceString = "${STARTTAG}<span
style=\"background-color:#FFFFCC\">${MATCHTERM}</span>${ENDTAG}";

Regex.Replace(htmlBody, searchTerm, replaceString,
RegexOptions.IgnoreCase);

I am trying to match the search term within HTML tags. i.e.

<htmltag>searchterm</htmltag>
Because of the .* (greedy) in ENDTAG it will match the last tag. Even if
you replace it with .*? (non-greedy) there are still some problems:

<h1> searchterm <b> searchterm </b> </h1>
<h1> searchterm <br> searchterm </h1>
<h1> searchterm searchterm </h1>

In all cases only one searchterm will be replaced.
If you have valid html, then you can say that a word isn't inside a tag if
the first following bracket is a < and not a >. So put together with a
positive lookahead this would become:

string searchTerm = lastSearchTerm + "(?=[^>]*<)";

string replaceString = "<span style=\"background-color:#FFFFCC\">"+
lastSearchTerm + "</span>";

It may still do wrong at title and scripts.

hth,
greetings


and then replace the search term with a span tag to color it, like so:

<htmltag><span
style=\"background-color:#FFFFCC\">searchterm</span></htmltag>

This works, but works inconsitently (and without a discernable pattern
when it fails).

So, does anyone see anything obviously wrong with my Regular
Expressions? I am pretty new to regular expressions, although I
usually know enough to get stuff done.

mike c

Nov 16 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: mike c | last post by:
I have a search app that searches local HTML files for a specified term. I then display the pages that contain the term. I would like to highlight the search term within the HTML when it is...
3
by: DDK | last post by:
I am trying to figure out how to Replace tags such as ... with the correct HTML <b>...</b> tags in C#. The code below works however only if one set of tags are found, if you have more than two...
1
by: George Durzi | last post by:
Consider this excerpt from some HTML. (This is a copy from View->Source, except for the comment) <TABLE WIDTH=100% CELLPADDING=0 CELLSPACING=0 border=0> <?xml version="1.0" encoding="UTF-16"?>...
1
by: darrel | last post by:
I have some vb.net code that is running a regex, matching groups, and replacing them. I'm trying to come up with a simple script that will strip all attributes from all HTML tags. This is what I...
13
by: Chris Lieb | last post by:
I am trying to write a regex that will parse BBcode into HTML using JavaScript. Everything was going smoothly using the string class replace() operator with regex's until I got to the list tag....
2
by: Tim_Mac | last post by:
hi, i have a tricky problem and my regex expertise has reached its limit. i have read other posts on this newsgroup that pull out the plain text from a html string, but that won't work for me...
2
by: Craig Buchanan | last post by:
I have a HTML fragment that looks like this: <tr> <td valign="top" nowrap><span class="textBold">Property ID: </span></td> <td valign="top" nowrap colspan="4"...
1
by: =?Utf-8?B?QWxCcnVBbg==?= | last post by:
I have a regular expression for capturing all occurrences of words contained between {{ and }} in a file. My problem is I need to capture what is between those symbols. For instance, if I have...
1
by: jonnyboy6969 | last post by:
Hi All Really hoping someone can help me out here with my deficient regex skills :) I have a function which takes a string of HTML and replaces a term (word or phrase) with a link. The pupose...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.