mostlylucid

scott galloway's personal blog...
posts - 916, comments - 758, trackbacks - 11

My Links

News

Archives

Post Categories

Misc. Coding

Finding words NOT in a piece of text

Pretty clever little function...can't think of a use for it right now :-)

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.

posted on Saturday, September 06, 2003 12:03 AM

Print | posted on Friday, September 05, 2003 7:17 PM | Filed Under [ .NET ]

Comments have been closed on this topic.

Powered by: