All Articles
Text

Capture a Domain From Email-Like Text With Regex

November 13, 20263 min read

The pattern @([^\s@]+) captures the text following an at sign up to whitespace or another at sign. For [email protected] [email protected], its groups are example.org and team.example.net.

Regex tester capturing domains from two sample addresses
  1. Open the Regex Tester and enter @([^\s@]+) as the pattern.
  2. Keep g - Global and Show Capture Groups checked. Enter [email protected] [email protected] as test text.
  3. Inspect the match list and capture groups: each full match starts with @, while group 1 contains the following domain-like text.
Pattern: @([^\s@]+)
Text: [email protected] [email protected]
Groups: example.org, team.example.net

Turn off g - Global to see only the first match, then turn it back on for both.

Regex tester with global matching disabled

This teaching pattern also accepts some malformed address-like text and can include trailing punctuation. It does not validate an email address, confirm a mailbox exists, or prove ownership.

If it doesn't work

  • Check that the parentheses and backslash are present and Show Capture Groups is enabled.
  • Enable g - Global if only the first sample appears.
  • Use an appropriate validation and confirmation process for real email addresses.
RegexDeveloper Tools