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.


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.


private string MatchHandler(Match m)



     return m.Value;



Leave a Reply

Your email address will not be published. Required fields are marked *