I am trying to validate the first part of an email address and I want to allow up to two dots, for example these would be valid:
I have tried this regex pattern, but it's only matching one dot
^([\w-]+[.-]?[']?[\w-]+)@([\w.-]+)$
How could I change the pattern to allow for up to two dots?
^([\w\-]+\.?){0,2}[\w\-]+@[\w.\-]+$
The trick is to write "You can have a non-dotted characters before the @" ([\w\-]+@), but before that, you can have 0 to 2 times a dot-ended sequence: ([\w\-]+\.?){0,2}
I suggest you to escape your -, as in some circumstances it is a special instruction.
If you want to save the group capture you suggested (Elvis operator ?: prevents utility groups to be captured):
^((?:[\w\-]*\.?){0,2}[\w\-]+)@([\w.-]+)$