Link to home
Start Free TrialLog in
Avatar of gmann001
gmann001

asked on

Regex - Replace all instances except when found within anchor tags or image tags

I'm trying to create a "search/replace" page within my site.  I would like to find a Regular Expression that will do the replacing in my web page content except when the word/phrase is found within an Anchor tag or Image tag or All tags.

Basically, my goal for the application is to have the search/replace ignore all HTML tags, and then by selecting  checkboxes, I can allow it to replace within the different types of tags.
Avatar of raterus
raterus
Flag of United States of America image

Ok, but what specifically do you need our help with?  What have you tried already?
Avatar of gmann001
gmann001

ASKER

I'm new to the whole RegEx world.  The pattern's I have tried so far are...

"(myPhrase)"      (which returns all matches including when found in html tags)
"(myPhrase)^(<a.*?>)"    (gave strange results, was hoping to match everything except anchor tags)
"(myPhrase)^(<img|a.*?>)"    (didn't work, was hoping to match everything except anchor and image tags)

Honestly, I don't know if I'm even going the right direction.
I'd keep a reference page handy, like so
http://www.regular-expressions.info/reference.html

ultimately, you are going to want to match tag and phrase, so I think your regex should look like this
<\w+>(myPhrase)

The Regex.Replace function actually has a great method to check specific tags.  You can actually tell the function to use another function to perform the actual replace, so you can have conditionally control over what to actually do.  Check out the longer examples near the end of this article.

http://msdn.microsoft.com/en-us/library/cft8645c.aspx
Unfortunately that has me more confused as ever.  I tried your pattern and am getting no results...

I tried a simple MatchEvaluator and I'm not sure if I understand it correctly.


Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
 
Dim patternSearchFor As String = "<\w+>(content)"
Dim regExOpt As RegexOptions = RegexOptions.IgnoreCase
Dim MatchEval As New MatchEvaluator(AddressOf regexReplacePhrase)
 
Dim fldPageContent As String = Regex.Replace("All of my content goes here", patternSearchFor, MatchEval, regExOpt)
 
 
label1.text = fldPageContent
End Sub
 
Private Function regexReplacePhrase(ByVal m As Match) As String
        Return "test"
End Function

Open in new window

In your search string there are no html tags!

try this,
"All of my <b>content</b> goes here"

slight upgrade to your regex too, this will match the start and the end tag,
"<(\w+).*>(content)</\1>"


I tried what you stated, and it worked...  however, I changed my phrase and my text (see snippet, and then it doesn't work.

Why does it not match anything now?


    Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
        Dim patternSearchFor As String = "<(\w+).*>(home)</\1>"
        Dim regExOpt As RegexOptions = RegexOptions.IgnoreCase
        Dim MatchEval As New System.Text.RegularExpressions.MatchEvaluator(AddressOf regexReplacePhrase)
 
        Dim fldPageContent As String = System.Text.RegularExpressions.Regex.Replace("my home is <a href=""home.html"" title=""test home"">at home</a>", patternSearchFor, MatchEval, regExOpt)
 
 
        label1.text = fldPageContent
    End Sub
 
    Private Function regexReplacePhrase(ByVal m As Match) As String
        Return "test"
    End Function

Open in new window

That regex expression is expecting everything between the tags to match, in this case it isn't.  What do you want to do in this case?
I have three checkboxes on my form...

1. replace within content
2. replace within links (includes href and img)
3. replace within html tags

The end-user will then enter the text to search for and text to replace with.

Depending on the selections of "where" to replace,  then the text gets replaced.

I'm assuming that within the regexReplacePhrase function I will need to make some type of comparison to the users selections and then determine whether to replace or not.
That's right, I'd have some If statements in the regexReplacePhrase that determine the checkboxes and performs the replace, if applicable.
Here is an example of what I was hoping to accomplish....

In the following text

<div id="homework">my home is <a href="home.html" title="test home">at home</a><img src="home.gif" alt="home image"></div>



I want to replace the word "home" with the word "REPLACED".  If I have the checkbox for "content only" selected (I consider the title of href's and alt of images as content), then the text would look like this...


<div id="homework">my REPLACED is <a href="home.html" title="test REPLACED">at REPLACED</a><img src="home.gif" alt="REPLACED image"></div>

If I have the checkbox for "links only" selected, then the text would look like...

<div id="homework">my home is <a href="REPLACED.html" title="test home">at home</a><img src="REPLACED.gif" alt="home image"></div>

And if I have the checkbox for "all other html" selected, then I am hoping to see...

<div id="REPLACEDwork">my home is <a href="home.html" title="test home">at home</a><img src="home.gif" alt="home image"></div>


And of course there can be any combination of checkbox selections. How would I fix the regex so that the above snippet would return a match?  Then, how would I add the IF/THEN statements within the regexReplacePhrase function? What would get passed to the function that allows me to do the If statement?

This is why I was thinking it would've been easier to create my regex dynamically using the | ("or").

thanks for your assistance
ASKER CERTIFIED SOLUTION
Avatar of raterus
raterus
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
What's the status here, did you find an answer, if so please close your question!
I think the last comment should be accepted under the premise "It can't be done".  
https://www.experts-exchange.com/help.jsp#hi96
It appears this can not be done.