Search code examples
regexregex-lookarounds

Regular expression to validate username section in email


I'm trying to create a regular expression in java to validate usernames against these criteria:

  • it can be started with alphanumeric character or _ or - or.
  • it should contain atleast two alphanumeric character at any position before @ symbol
  • after @ simple domain name validation

Example

I have created something like below one but it is not working for me. ^(?=.*[a-zA-Z0-9]{2})[a-zA-Z0-9_.-]+@[a-zA-Z0-9.-]+$


Solution

  • Your idea of using a lookahead is correct, but you are using . inside it, and . matches any character but line break characters. What you need is to restrict it to only match characters other than @. Also, the {2} limiting quantifier is applied to [a-zA-Z0-9] character class only, to match a disjoint alphanumeric twice, you need to group the characters other than alphanumeric and the alphanumeric char pattern.

    So, it can look like

    ^(?=(?:[^@a-zA-Z0-9]*[a-zA-Z0-9]){2})[a-zA-Z0-9_.-]+@[a-zA-Z0-9.-]+$
    

    See this regex demo.

    However, it might not be a usable email regex for many cases, mind that the simplest pattern to match a "potential" email is ^[^\s@]+@\S+$, and if you want to apply the same restriction to this pattern, it would look like ^(?=(?:[^@a-zA-Z0-9]*[a-zA-Z0-9]){2})[^\s@]+@\S+$.

    Some details

    • ^ - start of string
    • (?=(?:[^@a-zA-Z0-9]*[a-zA-Z0-9]){2}) - a positive lookahead that matches two consecutive occurrences of the following sequence: zero or more chars other than ASCII letters and digits and @ ([^@a-zA-Z0-9]*) and then an ASCII alphanumeric ([a-zA-Z0-9])
    • [a-zA-Z0-9_.-]+@[a-zA-Z0-9.-]+ - one or more ASCII letters, digits, underscores, dots or hyphens, then a @ char, and then again one or more ASCII letters, digits, underscores, dots or hyphens
    • $ - end of string.