Skip to content

Instantly share code, notes, and snippets.

@bert
Created May 4, 2025 14:41
Show Gist options
  • Save bert/fb50c3435025c99188d271d2093b0080 to your computer and use it in GitHub Desktop.
Save bert/fb50c3435025c99188d271d2093b0080 to your computer and use it in GitHub Desktop.
E-mail address regex

E-mail address regex

by Suvam Das

Alright, let's break down that regular expression!

^ [ \ w - \ . ] + @ ( [ \ w - ] + \ . ) + [ \ w - ] { 2,4 } $

Here's a step-by-step explanation:

^: This symbol asserts that the match must start at the beginning of the string.

[ \ w - \ . ] +: This part matches one or more (+) occurrences of any character that is:

\w: A word character (letters, numbers, and underscore).

-: A hyphen.

.: A literal dot (period).

@: This matches a literal "@" symbol.

( [ \ w - ] + \ . ) +: This is a capturing group (due to the parentheses) that matches one or more (+) repetitions of the following:

[ \ w - ] +: One or more word characters or hyphens.

.: A literal dot.

[ \ w - ] { 2,4 }: This matches between 2 and 4 ({2,4}) occurrences of word characters or hyphens.

$: This symbol asserts that the match must end at the end of the string.

In essence, this regular expression has the general structure of a simplified email address.

It starts with one or more word characters, hyphens, or dots.

Then, it has an "@" symbol.

After the "@", it has one or more parts separated by dots, where each part consists of one or more word characters or hyphens.

Finally, it ends with a top-level domain (like "com", "org", "net", "in", etc.) that is between 2 and 4 characters long and consists of word characters or hyphens.

So, a programmer might see a pattern that looks suspiciously like the basic format of an email address!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment