Stuck with Regular Expression code to apply HTML tag to text, but exclude if inside <?> Tag
Possible duplicate:
Open RegEx tags, excluding standalone XHTML tags
I'm trying to write a little regex that will loop through some text written by our editors and apply the tag <acronym>
to the first instance it finds, in the abbreviation we stick to in our Glossary of Terms. "
So, for this example, I used an acronym ITS
.
The first thing I thought was to make an example with a combination of scenerios that I could test with, i.e. ITS
sit with punctuation, in HTML tags and the ones we've already applied this to (in other words, the script skipped this before, so no more need to do).
I'm almost there but just stuck at the last point: - (.
Here's the regex I got so far - <[^<|]+?>?>ITS<[^<]+?>|ITS
Example - FROM (EACH ITS IN BOLD FOR PROCESSING WITH ACRONYM):
I want you to tag this
ITS, but not this wrapped one - <acronym title="ITS" id="thisIsATest">ITS</acronym>
This is another test as I still want to update
<p>
ITS</p>
which has other HTML tags wrapped around them.ITS want those who launch sentences to complete ITS . ITS and those wrapped in punctuation.
Test link:
<a href="index.cfm>ITS</a>
AND I WANT TO CHANGE IT:
I want you to tag this <acronym title="ITS">ITS</acronym>
but not this one wrapped -<acronym title="ITS">ITS</acronym>
This is another test as I still want to update <acronym title="ITS">ITS</acronym>
which have other HTML tags wrapped around them.
<acronym title="ITS">ITS</acronym> want ones that start sentences and ones that finish <acronym title="ITS">ITS</acronym>. <acronym title="ITS">ITS</acronym>, and ones which are wrapped in punctuation.
Test link:
<acronym title="ITS"><a href="index.cfm>ITS</a></acronym>
Are there any Reg Ex experts out there who could help me finish this? Any other hints hints would be appreciated as well.
** UPDATE ** Don't know if this helps, but it will only be found at this point:
<acronym[^<]*ITS</acronym>
and this will find all ITS:
<[^<]*>ITS<[^<]*>|ITS
What I really need is to combine them to say find all ITS, but exclude them in tags.
Thanks a lot James
PS This will be put into a ColdFusion application if it helps anyone with the specific syntax.
Here's the HTML I'm trying to parse:
a source to share
Here's your main problem: regex is not a parser. This issue has been addressed many times and there is no general regex-only solution. You can fake it up to a certain point using lookahead, lookbehind and some really tricky legs, but you quickly get to the point where your expression is difficult to maintain.
I can suggest a couple of approaches.
If you are using XML compatible text, you can parse the text using xmlparse () and then step through the resulting structure by applying your regex to the xmltext of each node.
Alternatively, you can try replacing each tag in the text block with a placeholder, replacing in the resulting text, and then restoring the placeholders.
Obviously, none of these are perfect, but with some tweaking, they can get you where you go.
a source to share
It works?
(?!(<acronym\W*>|\w))ITS(?!(<acronym\W*>|\w))
Not tested since I don't have ColdFusion
Looks like ColdFusion doesn't support lookbehinds. However, you can still use lookaheads ( (?!...)
) to ensure that for a string (ITS) it shouldn't </acronym>
.
\\WITS(?!(</acronym\\W*>|\\w))
Since you cannot use lookbehinds, you need to \ W at the beginning to make sure the string is not part of another word. Unfortunately, it will eat the previous character if it matches. The \ W at the end also ensures it's not part of a word.
a source to share