Search code examples
phpregexemailemail-validation

how this regular expression expand and works?


I want to know how this regular expression is expand and how it validates proper E-mail address ?

"^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)↪*(\.[a-z]{2,3})$"

Solution

  • What is a valid email address? It's usually name@FQDN

    Whereby FQDN is "fully qualified domain name" A FQDN must consist of one hostname and one top-level domain. It CAN have one or more optional subdomains.

    ^ start of match (see below)

    [_a-z0-9-]+ the very first character of a valid email account name (required, hence the + qualifier 1..n)

    (\.[_a-z0-9-]+)* optional characters for a valid email account name (hence the * quantifier 0..n)

    @ @-literal (delimiting account name from FQDN)

    [a-z0-9-]+ character set for domain names (second-level domains / subdomain)

    (\.[a-z0-9-]+)* character set for domain names (second-level domains / subdomain)

    (\.[a-z]{2,3}) character set for top level domains (.com, .net, etc. - won't be useful for the 'new' TLDs like .info, .business, .museum and so on

    $ end of match

    That ^ . . . $ is often used to declare that the whole string must consist of the pattern (and not just include it somewhere).