locked
One of two Possibilities

    Question

  • I have email addresses that fall into one or two types.

    abcde@anywhere.anytime.com
    abc.def@anywhere.com

    what comes after the @ is not significant.

    I want to capture abcde as ?<lastname>(abcde) or  ?<firstname>abc and ?<lastname>def

    Thanks
    Friday, December 19, 2008 11:53 AM

Answers

  • Try: @"(?<firstname>[a-z\d_-]+)(?:\.(?<lastname>[a-z\d_-]+))?@(?:[\w-]+\.)+[a-z]{2,4}"

    Beware that the lastname group will always be captured, even for the 1st case. Therefore to check which case it is you should compare the lastname group with String.Empty to see if they're equal or not and decide how to proceed.

    You can see what I'm referring to in a regex editor such as Expresso. This would also help visualize your other question about (?:) usage. There are other editor options as well, check the sticky thread at the top of this forum for .net regex resources.
    Document my code? Why do you think it's called "code"?
    • Marked as answer by Mattaniah Friday, December 19, 2008 3:39 PM
    Friday, December 19, 2008 3:23 PM