Posted: February 1, 2006 in Uncategorized

Regex makes me twitch. The syntax just seems designed to make you want to claw your brain out with a garden trowel.


An email validation regex (this is all one line of code. Yes really):


(?:(?:\r\n)?[ \t])*(?:(?:(?:[^()@,;:\\".\[\]00-31]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\["()@,;:\\".\[\]]))|"(?:[^\"\r\\]|\\.|(?:(?:\r\n)?[\t]))*"(?:(?: \r\n)?[ \t])*)(?:\.(?:(?:\r\n)?[\t])*(?:[^()@,;:\\".\[\] 00-31]+(?:(?:( ?:\r\n)?[\t])+|\Z|(?=[\["()@,;:\\".\[\]]))|"(?:[^\"\r\\]|\\.|(?:(?:\r\n)?[\t]))*"(?:(?:\r\n)?[ \t])*))*@(?:(?:\r\n)?[ \t])*(?:[^()@,;:\\".\[\]00- 31]+(?:(?:(?:\r\n)?[\t])+|\Z|(?=[\["()@,;:\\".\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*)(?:\.(?:(?:\r\n)?[ \t])*(?:[^()@,;:\\".\[\]00-31]+ (?:(?:(?:\r\n)?[\t])+|\Z|(?=[\["()@,;:\\".\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*))*|(?:[^()@,;:\\".\[\] 00-31]+(?:(?:(?:\r\n)?[\t])+|\Z |(?=[\["()@,;:\\".\[\]]))|"(?:[^\"\r\\]|\\.|(?:(?:\r\n)?[\t]))*"(?:(?:\r\n) ?[ \t])*)*\<(?:(?:\r\n)?[\t])*(?:@(?:[^()@,;:\\".\[\] 00-31]+(?:(?:(?:\ r\n)?[\t])+|\Z|(?=[\["()@,;:\\".\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[\t])*)(?:\.(?:(?:\r\n)?[ \t])*(?:[^()@,;:\\".\[\]00-31]+(?:(?:(?:\r\n) ?[\t])+|\Z|(?=[\["()@,;:\\".\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[\t]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[ \t])*)(?:\.(?:(?: \r\n)?[\t])*(?:[^()@,;:\\".\[\] 00-31]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\["()@,;:\\".\[\]]))|"(?:[^\"\r\\]|\\.|(?:(?:\r\n)?[\t]))*"(?:(?:\r\n)?[ \t]) *))*@(?:(?:\r\n)?[ \t])*(?:[^()@,;:\\".\[\]00-31]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\["()@,;:\\".\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[\t])*)(?:\ .(?:(?:\r\n)?[ \t])*(?:[^()@,;:\\".\[\]00-31]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\["()@,;:\\".\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[\t])*))*\>(?:( ?:\r\n)?[ \t])*))*)?;\s*)

Advertisements
Comments
  1. chloralone says:

    True, but much of it is entirely pointless. Still, the best email regex I’ve come up with is still perhaps 120 chars. If you want to cover all possible roman lettered email addresses, it’s a bitch.

  2. snoboy says:

    OMG!!! WTF??? That’s painful…

  3. Anonymous says:

    OMG!!! WTF??? That’s painful…

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s