Got more questions? Find advice on: ASP | SQL | XML | Windows
in Search
Welcome to RegexAdvice Sign in | Join | Help

search for word not in tags- replace with link

Last post 08-14-2008, 9:35 AM by ddrudik. 8 replies.
Sort Posts: Previous Next
  •  08-13-2008, 12:18 AM 45299

    search for word not in tags- replace with link

    I am new to using regular expressions, so forgive me if this sounds like a really bonehead question. 

    I have an html doc in which I want to replace all instances of the word "limo" that occur after the <body> tag (so it doesn't pick up my javascript or CSS) and outside of all other tags (so it doesn't screw up my links and styles) and I want to replace it with this:

    <a href="limo.html">limo</a>

    The purpose is to only affect the text the visitor sees on the page so all "limo" instances become links.

    The application I'm using is HandyFile Find and Replace 

    Thank you in advance for your time!

    -Elizabeth

  •  08-13-2008, 6:52 AM 45303 in reply to 45299

    Re: search for word not in tags- replace with link

    limo(?=([^<>]*<[^>]+>[^<>]*)+$|[^<>]*$)(?=((?!<body).)*</body>)

     

    Unfortunatelly it doesn't work with HandyFile


    http://portal-vreme.ro
  •  08-13-2008, 10:04 AM 45306 in reply to 45303

    Re: search for word not in tags- replace with link

    Thanks killahbeez!  You are wonderful!

    Can you recommend an application that your expression will work in?  I only have HandyFile because it was free and the first app I found.

    Thankyouthankyouthankyou!!!

    But wait, do you know how the "replace" expression should be written?  I imagine if I just put

    <a href="limo.html">limo</a>

    in the replace box, it won't work- or will it? 

     

    Thanks again,

    Elizabeth 

  •  08-13-2008, 10:14 AM 45307 in reply to 45306

    Re: search for word not in tags- replace with link

    RegexBuddy, Regex Coach etc. (any other application which use PCRE engine), and yes you will replace it with

     <a href="limo.html">limo</a>


    http://portal-vreme.ro
  •  08-13-2008, 10:53 AM 45309 in reply to 45307

    Re: search for word not in tags- replace with link

    Wouldn't that pattern also match limo inside href's such as:

    <a href="test.htm">limo</a>

    eaorr, you might consider the freeware notetab light, note that any non-freeware options it may enable are not required for the replacement as all versions support PCRE search/replace.

    When you use kb's pattern there you will want to include the singleline switch as:

    (?s)limo(?=([^<>]*<[^>]+>[^<>]*)+$|[^<>]*$)(?=((?!<body).)*</body>)

    not that notetab light is the best program out there, just the first free one I found.

    http://www.notetab.com/index.php


  •  08-13-2008, 11:32 AM 45310 in reply to 45309

    Re: search for word not in tags- replace with link

    Possibly something like this slight modification should exclude anchor tags to avoid double-href's:

    (?si)limo(?![^<>]*</a>)(?=([^<>]*<[^>]+>[^<>]*)+$|[^<>]*$)(?=(?:(?!<body).)*</body>)

    Seems to test ok in notetab light.


  •  08-13-2008, 10:34 PM 45328 in reply to 45310

    Re: search for word not in tags- replace with link

    Thanks ddrudik!

    I must be doing something wrong- I copied your line

    (?s)limo(?=([^<>]*<[^>]+>[^<>]*)+$|[^<>]*$)(?=((?!<body).)*</body>

    and pasted it in the "find" field, ran the search on one of my html docs chock full of "limo" in the text, but it didn't find anything.

     The GUI on NoteTab Light is quite different than HandyFile.  I do want to use the "replace" function under "Search", right?  Or is there a beefier version I should be looking for?

     

    Thanks for all your help!!

    -eaorr 

  •  08-13-2008, 10:34 PM 45329 in reply to 45310

    Re: search for word not in tags- replace with link

    Thanks ddrudik!

    I must be doing something wrong- I copied your line

    (?s)limo(?=([^<>]*<[^>]+>[^<>]*)+$|[^<>]*$)(?=((?!<body).)*</body>

    and pasted it in the "find" field, ran the search on one of my html docs chock full of "limo" in the text, but it didn't find anything.

     The GUI on NoteTab Light is quite different than HandyFile.  I do want to use the "replace" function under "Search", right?  Or is there a beefier version I should be looking for?

     

    Thanks for all your help!!

    -eaorr 

  •  08-14-2008, 9:35 AM 45345 in reply to 45329

    Re: search for word not in tags- replace with link

    With a text file:

    <html>
    limo
    <head>
    limo
    </head>
    limo
    <body>
    limo
    <a href="limo.HTML">limo</a>
    limo
    <a href="limo.HTML">limo</a>
    limo
    </body>
    limo
    </html>

    And this pattern (note your pasted pattern was missing a closing parens):

    (?s)limo(?=([^<>]*<[^>]+>[^<>]*)+$|[^<>]*$)(?=((?!<body).)*</body>)

    Search->Replace->Replace All

    Note it will match/replace all instances of limo within the body outside of HTML <> tags.  My issue with that was what if limo already existed inside a href tag that would be an issue.


View as RSS news feed in XML