regexvalidation

Regular Expression for Multiple Email addresses separated with a single space and not ending with space


I want to come up with a Regular Expression for multiple email addresses separated by a single space and not ending with space and it should accept one or more emails.

The email validation and the double space are working fine with me but I am stuck with the "not ending with space" part.

I am using below which is working for everything mentioned expect that it MUST have an ending space:

^((?:([A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4})\b {1}))+$

Solution

  • two possible approaches

    the first approach would also match for two emails separated with no space. But as you also have the \b before the space in your regex the validation will be ok with this simpler first approach. Instead of \b {0,1} you could also write \b ?

    ^((?:([A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4})\b {0,1}))+$
    

    or

    ^((?:([A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4})\b {1}))*((?:([A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4})\b)+$
    


    short answer (combined with ideas from other answers)

    ^(?:[\w.%+-]+@[\w.]+\.[A-Za-z]{2,4}\b ?)+$