Search code examples
javaregexjakarta-eeemail-validation

Regex Expression for finding a string Which Has one @ and should not start with . or end with


Regex Expression for finding a string which has one @ and should not start with . or end with . Meaning hello@Same is valid .hello@Same, hello@Same. and hello@sdj@same are all invalid

What is the problem in (^([^@]+)@([^@])+$)(^[^\\.].*$)(^.*[^\\.]$).

All these three parts work individually but when we put together doesn't work


Solution

  • You can't just concatenate regular expressions if you want to match all of them.

    Another thing that is wrong is trying to validate email addresses with a regular expression. This just does not work well. The only way to find out if the user entered a valid adress is to send an email.

    A regular expression that matches your specifications would be

    ^(?=[^@]+@[^@]+$)[^.].*[^.]$
    

    but, as I said, thats not a great way to validate email adresses. Check this link for horrible counter examples. =)