Skip to content

Instantly share code, notes, and snippets.

@daurnimator
Created July 4, 2012 00:11
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save daurnimator/3044300 to your computer and use it in GitHub Desktop.
Save daurnimator/3044300 to your computer and use it in GitHub Desktop.
lpeg email pattern
local P = lpeg.P
local R = lpeg.R
local S = lpeg.S
local V = lpeg.V
local C = lpeg.C
local CHAR = R"\0\127"
local SPACE = S"\40\32"
local CTL = R"\0\31" + P"127"
local specials = S[=[()<>@,;:\".[]]=]
local atom = (CHAR-specials-SPACE-CTL)^1
local dtext = CHAR - S"[]\\\13"
local qtext = CHAR - S'"\\\13'
local quoted_pair = "\\" * CHAR
local domain_literal = P"[" * ( dtext + quoted_pair )^0 + P"]"
local quoted_string = P'"' * ( qtext + quoted_pair )^0 * P'"'
local word = atom + quoted_string
-- Implements an email "addr-spec" according to RFC822
local email = P {
V"addr_spec" ;
addr_spec = V"local_part" * P"@" * C(V"domain") ;
local_part = word * ( P"." * word )^0 ;
domain = V"sub_domain" * ( P"." * V"sub_domain" )^0 ;
sub_domain = V"domain_ref" + domain_literal ;
domain_ref = atom ;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment