Solved

Regular expression to exclude HTML tags from text replacement

Posted on 2004-09-28
7
539 Views
Last Modified: 2011-09-20
i need to create a regular expression (in Java) to replace sections of text that are NOT in <span> HTML tags. basically i am replacing a set of terms with a block of text inside span tags, but i don't want the terms inside those blocks of text to be matched as well. so i only want to replace terms that aren't inside <span> tags.

My regular expression so far is:

((<span.*/span>)*^(<span)) ($term)

where $term is the term that i want to replace. basically the regular expression needs to say "search for all instances of $term that are not between an open and end <span> tag (and replace with the defined text)".

please ask if i haven't explained this adequately and i can explain the situation more clearly.

any help would be appreciated,

Stafford

p.s. i'm not all that good at Regular Expressions, so please excuse the one above ;)
0
Comment
Question by:staffordvaughan
  • 3
  • 3
7 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 250 total points
ID: 12177540
Regular expressions are not particularly good for *not* matching things. What i'd suggest is the something like the following:

final String RE_ANY_TAG = "(<[A-Za-z\\-]+>)([^<]+)(<[/A-Za-z\\-]+>)";

then check the first group does *not* contain "<span>"

0
 

Author Comment

by:staffordvaughan
ID: 12177576
thanks for your response, but can you be more specific please?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12177623
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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.

 

Author Comment

by:staffordvaughan
ID: 12187223
the question is still open, i would like for someone who is good at regular expressions to actually post the regular expression solution if possible. i'm familiar with regular expressions in general, but the actual solution to this question is eluding me.

or perhaps suggest a more appropriate category to post this question on? i could not find a better one than this.

thanks
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12188488
>>to actually post the regular expression solution if possible

That's quite a bit of work! What are talking about specifically here:

>>
basically i am replacing a set of terms with a block of text inside span tags, but i don't want the terms inside those blocks of text to be matched as well. so i only want to replace terms that aren't inside <span> tags.
>>
0
 

Author Comment

by:staffordvaughan
ID: 12400018
i accepted the first response as the answer even though it wasn't really what i was after. as you say though, the specific regular expression is probably a lot of work and my explanation might not have been sufficient. thanks for your help anyway, as it turned out i used a different (non regular expression) method for doing the task because i just couldn't work out the regular expression.

thanks,
Stafford
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

Suggested Solutions

Title # Comments Views Activity
How to get all the API from website? 11 104
Running JavaFX on JDeveloper 12C 1 54
ejb stateless example 2 21
add projects t working set in maven 2 20
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

856 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