Attention: We are retiring the ASP.NET Community Blogs. Learn more >

Find words NOT IN text

I'm currently writing a series of articles (that will hopefully get published {grin}) titled "Regex Reminders" that will provide dozens of code snippets that demonstrate how to perform common - and some not-so-common - operations using regex.

In the first article titled "Replacing" I came up with a script that shows how to highlight a word when NOT found within another piece of text.  The following code snippet highlights that...

Match "foo" not in ANCHORS using MatchEvaluator

[C#]
string
source = sourceTextBox.Text ;
Regex re = new Regex( @"(?'Url'<a [^>]*>.*?</a>)|(?'theWord'foo)" ) ;
// use a MatchEvaluator with a pointer to the delegate method
string result = re.Replace( source, new MatchEvaluator( FormatLinkBits ) ) ;


// delegate method
private string FormatLinkBits( Match m )
{
    if( m.Groups["theWord"].Success )
    {
        string theWord = m.Groups["theWord"].Value ;
        return "<b>" + theWord + "</b>" ;
    }
    else
        return m.Value ;
}

This would be useful for things like turning words into hyperlinks - similar to the "KeyWords" feature that ScottW recently implemented into version 0.94 of .Text.

1 Comment

Comments have been disabled for this content.