Web developers wiki ASP.NET Sitecore Sharepoint Kentico by Evident Interactive

Regular Expressions and Matchhandlers

Modified: 2008/11/18 10:34 by mark - Categorized as: ASP.NET, Csharp


For a project I need te get the email adresses out of a string with all kinds of text in it. After a lot of trying I found the matchhandler method. It is quite easy, you just need to adjust the Replace command and add a MatchEvaluator. Then you can use the matchhandler to do anything with the right string. In this case I´m adding it to a generic list.

C#:
Regex.Replace(source, @"([a-zA-Z_0-9.-]+\@[a-zA-Z_0-9.-]+\.\w+)", new MatchEvaluator(MatchHandler)); 

And add the string to the generic list.

C#:
private string MatchHandler(Match m) 
{ 
     col.Add(m.Value); 
     return m.Value; 
} 

source: NewGuid.Net Blog

 © Evident Interactive BV