Regular Expressions 101

Community Patterns

RFC5322 email address validation (maybe FEWER STEPS !)

1

Regular Expression
PCRE2 (PHP >=7.3)

/
^(?>(?>[ \t]++(?>\r\n[ \t]++)*+|\r\n[ \t]++)?+(?>(?>(\((?>(?>[ \t]++(?>\r\n[ \t]++)*+|\r\n[ \t]++)?+(?>[\x01-\x08\x0b\x0c\x0e-\x1f!-'*-\[\]-\x7f]++|\\[\x00-\x7f]|(?-1)))*+(?>[ \t]++(?>\r\n[ \t]++)*+|\r\n[ \t]++)?+\))(?>[ \t]++(?>\r\n[ \t]++)*+|\r\n[ \t]++)?+)++)?+)?+(?>(?>[\w\d!#-'*+\/=?^`{-~-]++|(?>"(?>(?>[ \t]++(?>\r\n[ \t]++)*+|\r\n[ \t]++)?+(?>[\x01-\x08\x0b\x0c\x0e-\x1f!#-\[\]-\x7f]++|\\[\x00-\x7f]))*+(?>[ \t]++(?>\r\n[ \t]++)*+|\r\n[ \t]++)?+"))(?>(?>[ \t]++(?>\r\n[ \t]++)*+|\r\n[ \t]++)?+(?>(?>(\((?>(?>[ \t]++(?>\r\n[ \t]++)*+|\r\n[ \t]++)?+(?>[\x01-\x08\x0b\x0c\x0e-\x1f!-'*-\[\]-\x7f]++|\\[\x00-\x7f]|(?-1)))*+(?>[ \t]++(?>\r\n[ \t]++)*+|\r\n[ \t]++)?+\))(?>[ \t]++(?>\r\n[ \t]++)*+|\r\n[ \t]++)?+)++)?+)?+(?>\.(?>(?>[ \t]++(?>\r\n[ \t]++)*+|\r\n[ \t]++)?+(?>(?>(\((?>(?>[ \t]++(?>\r\n[ \t]++)*+|\r\n[ \t]++)?+(?>[\x01-\x08\x0b\x0c\x0e-\x1f!-'*-\[\]-\x7f]++|\\[\x00-\x7f]|(?-1)))*+(?>[ \t]++(?>\r\n[ \t]++)*+|\r\n[ \t]++)?+\))(?>[ \t]++(?>\r\n[ \t]++)*+|\r\n[ \t]++)?+)++)?+)?+(?>[\w\d!#-'*+\/=?^`{-~-]++|(?>"(?>(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0e-\x1f!-'*-\[\]-\x7f]++|\\[\x00-\x7f]|(?-1)))*+(?>[ \t]++(?>\r\n[ \t]++)*+|\r\n[ \t]++)?+\))(?>[ \t]++(?>\r\n[ \t]++)*+|\r\n[ \t]++)?+)++)?+)?+[\w\d!#-'*+\/=?^`{-~-]++(?>(?>[ \t]++(?>\r\n[ \t]++)*+|\r\n[ \t]++)?+(?>(?>(\((?>(?>[ \t]++(?>\r\n[ \t]++)*+|\r\n[ \t]++)?+(?>[\x01-\x08\x0b\x0c\x0e-\x1f!-'*-\[\]-\x7f]++|\\[\x00-\x7f]|(?-1)))*+(?>[ \t]++(?>\r\n[ \t]++)*+|\r\n[ \t]++)?+\))(?>[ \t]++(?>\r\n[ \t]++)*+|\r\n[ \t]++)?+)++)?+)?+)*+|\[(?>(?>[ \t]++(?>\r\n[ \t]++)*+|\r\n[ \t]++)?+(?>[\x01-\x08\x0b\x0c\x0e-\x1f!-Z^-\x7f]++|\\[\x00-\x7f]))*+(?>[ \t]++(?>\r\n[ \t]++)*+|\r\n[ \t]++)?+\](?>(?>[ \t]++(?>\r\n[ \t]++)*+|\r\n[ \t]++)?+(?>(?>(\((?>(?>[ \t]++(?>\r\n[ \t]++)*+|\r\n[ \t]++)?+(?>[\x01-\x08\x0b\x0c\x0e-\x1f!-'*-\[\]-\x7f]++|\\[\x00-\x7f]|(?-1)))*+(?>[ \t]++(?>\r\n[ \t]++)*+|\r\n[ \t]++)?+\))(?>[ \t]++(?>\r\n[ \t]++)*+|\r\n[ \t]++)?+)++)?+)?+)$
/
gm

Description

validate email address according to RFC 5322 specification. I tried to optimize regex radically restructuring the original BNF-like specification with full support preserved.

Submitted by hirunotuki - a month ago