Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 545
  • Last Modified:

Regular expression to exclude HTML tags from text replacement

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
staffordvaughan
Asked:
staffordvaughan
  • 3
  • 3
1 Solution
 
CEHJCommented:
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
 
staffordvaughanAuthor Commented:
thanks for your response, but can you be more specific please?
0
 
CEHJCommented:
0
Independent Software Vendors: 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!

 
staffordvaughanAuthor Commented:
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
 
CEHJCommented:
>>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
 
staffordvaughanAuthor Commented:
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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now