Regular Expressions and Matchhandlers

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.

[code:c#]

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

[/code]

And add the string to the generic list.

[code:c#]

private string MatchHandler(Match m)

{

     col.Add(m.Value);

     return m.Value;

}

[/code]

Leave a Reply

Your email address will not be published.