in reply to RexExp help: Highlight keywords in CGI search results, unless inside an HTML tag

Completely un-optimal, completely un-tested.. but I think I can get away with something like that from your code?

$newline =~ s|(?<!\<)$keyword(?!\>)|<font style="BACKGROUND: #ffffff"> +$keyword</font>|img;

The (?<!\<) implies that this shouldn't match a < before the $keyword, and the (?!\>) shouldn't match a > after it... so it's not in a tag.(I guess)


He who asks will be a fool for five minutes, but he who doesn't ask will remain a fool for life.

Chady | http://chady.net/

Replies are listed 'Best First'.
Re: Re: RexExp help: Highlight keywords in CGI search results, unless inside an HTML tag
by suaveant (Parson) on Jan 15, 2002 at 02:23 UTC
    There is nothing wrong with having a < before or a > after it.. so long as the < has a > after it before and vice versa for after.... so I don't think yours will quite work...
    I came up with the following... which seems to work at least somewhat...
    s|(\Q$text\E)(?![^<]*>)|<I>$1</I>|gi;
    which matches the text so long as it is not followed by a > before it sees a <

    Update \me laughs as joealba and I both post the code I gave him at the same time...

                    - Ant
                    - Some of my best work - (1 2 3)

Re: Re: RexExp help: Highlight keywords in CGI search results, unless inside an HTML tag
by joealba (Hermit) on Jan 15, 2002 at 02:20 UTC
    Thanks, Chady!

    suaveant gave me this little hunk of code that seems to be working great:
    $text =~ s|($keyword)(?![^<]*[>\Z])|<B>$1</B>|img;
    I've never used that explicit lookahead before. I love/hate it when I'm shown something so useful (when I should have known it before). I KNOW I read pp. 228-230 of Mastering Regular Expressions one day last year... :)