Regex Guide · Prompt: “Match valid email addresses and capture the local username and domain in separate groups”

Regular Expression for Email Validation & Extraction

Verified by LiveFormulaLab Engineering Team • Updated November 2026

Validate and extract email addresses with a clean regular expression. Test against valid and invalid emails live with capture groups.

Verified Regex Syntax

([a-zA-Z0-9._%+-]+)@([a-zA-Z0-9.-]+\.[a-zA-Z]{2,})

How This Formula Works (Step-by-Step)

A practical email regex balances accuracy with readability across JavaScript, Python, SQL, and Google Sheets.

Step 1: ([a-zA-Z0-9._%+-]+) captures the local mailbox name before the @ sign.

Step 2: @([a-zA-Z0-9.-]+\.[a-zA-Z]{2,}) captures the hostname and requires at least a 2-letter top-level domain.

Formula Syntax & Arguments Breakdown

Matches the mailbox local-part, literal "@" separator, and domain hostname with a 2+ letter top-level extension, separating username and domain into dedicated capture groups for form validation or CRM enrichment.

Argument / ClauseRequirementData TypeRole & Behavior
([a-zA-Z0-9._%+-]+)RequiredCapture Group 1Matches one or more valid local mailbox characters including dots, underscores, hyphens, and plus tags.
@RequiredLiteral DelimiterRequires the single "@" separator between local username and domain hostname.
([a-zA-Z0-9.-]+\.[a-zA-Z]{2,})RequiredCapture Group 2Captures the domain name and enforces at least one dot plus a 2+ letter top-level domain.
^ ... $ anchors (optional)OptionalLine BoundariesWrap the pattern in ^ and $ when validating standalone input boxes; omit anchors when extracting emails inside longer paragraphs.

3 Real-World Use Cases

  • Validating signup form email inputs in frontend and backend JavaScript.
  • Extracting email addresses from messy customer support transcripts.
  • Auditing CRM CSV uploads in Google Sheets with REGEXMATCH.

Cross-Platform Equivalents

Excel
=REGEXEXTRACT(A2, "([a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,})")
SQL
SELECT email FROM users WHERE email ~* '^[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,}
#39;;
Python Pandas
df[df['email'].str.match(r'^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}
#39;)]

Common Mistakes & Pro Tips

Pitfalls & How to Fix Them

  • Mistake: Validating form inputs without ^ and $ anchors accepts strings like "bad email: a@b.com!!!" because a substring matched.

    Fix: Add ^ at the start and $ at the end when validating standalone email fields.

  • Mistake: Forgetting to escape the dot (.) before the TLD allows any character (like a@bXcom) to match.

    Fix: Always escape literal dots as \. inside regular expressions.

  • Mistake: Restricting the TLD to {2,4} rejects modern valid extensions like .cloud, .digital, or .company.

    Fix: Use {2,} without an upper cap so newer multi-letter domains pass cleanly.

Engineering Pro Tips

  • Always trim whitespace before testing user input against an anchored email regex.
  • Use HTML5 validation or a syntax regex for instant feedback, backed by a verification email for true deliverability.
  • In Google Sheets, =REGEXMATCH(A2, "^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}
    quot;) flags invalid rows in bulk.
  • Normalize captured domains to lowercase in SQL or Python before counting unique company accounts.

Frequently Asked Questions

Should I anchor the regex with ^ and $ when validating form fields?
Yes, wrap the pattern as ^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$ to require the whole string to be one email.
Does this allow plus-addressed emails like name+tag@company.com?
Yes, the + character is explicitly included in the first character class.
How do I run this inside Google Sheets?
Use =REGEXMATCH(A2, "^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}
quot;).

Related Formulas & Guides