First off [_a-z0-9]+
is going to match the username fields for the majority of those testcases. Anything further testing of username field content will result in a mismatch. If you write a pattern that expects two .
-delimitered fields, it'll match when you provide two .
-delimitered fields and only then, not anything else. Make a mental note of that. I think you probably meant to put the .
in the first character set, and omit this part here: (.[_a-z0-9]+)
...
As for the domain part of the email address, similar story there... if you're trying to match domains containing two labels (yahoo
and com
) against a pattern that expects three... it's going to fail because there's one less label, right? There are domain names that only contain one label which you might want to recognise as email addresses, too, like localhost
...
You know, there is a point to where you can dig yourself down a very deep rabbit hole trying to parse email addresses, much to the effect of this question and answer sequence. If you're making this complex using regular expressions... I think maybe a better tool is a proper parser generator... otherwise, write the following
:
- A pattern that matches anything up until an
@
character
- A pattern that matches the
@
character (this will help you learn how to avoid your .
-related error)
- A pattern that matches everything (this will help you understand your
.
-related error)
- Combine the three above in the order presented.