Solved

Regular expression to exclude HTML tags from text replacement

Posted on 2004-09-28
7
543 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
[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
  • 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
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 

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

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Suggested Courses

623 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