You have a block of text that is not a mailing list: a server log, the source of a web page, an exported thread of comments. Somewhere inside it are the email addresses you actually need, mixed in with everything else, and copying them out by hand is slow, boring and easy to get wrong. A regular expression solves the finding part in seconds: one pattern describes the shape of an email address, and every place where the text matches that shape is a candidate address.

This guide shows how to do it with the free Regex Tester on Ovatly, how to read its match list as your extraction result, and how to tighten the pattern when it catches too much or too little.

Contents

  • Quick answer
  • What extraction means in a regex tester
  • The pattern, piece by piece
  • How to extract email addresses, step by step
  • Worked example: three addresses in a three-line text
  • Tightening the pattern: too much, too little
  • Limitations
  • Common mistakes
  • FAQs
  • Conclusion

Quick answer

Open the Regex Tester, paste your text, and use the pattern [\w.+-]+@[\w-]+(\.[\w-]+)+ with the flag g in the Flags field. The g flag matters: without it the tester reports only the first match, and with it you get every match in a list showing the matched text, its position in the text and any capture groups. Read that list as your extracted addresses, then adjust the pattern if it catches fragments or misses forms you care about. The pattern finds strings that look like email addresses; it cannot prove that any of them is a real, working address.

What extraction means in a regex tester

The Regex Tester runs a JavaScript regular expression against your text live and lists every match. Extraction is simply that list, read as a result set: each entry shows the text that matched, the index where it starts, and the capture groups inside it. Groups are more useful than they first look. With a pattern like (\w+)@(\w+)\.com run against "[email protected]", the tester reports the match with groups "ada" and "example", so you can see the two halves of an address separately instead of as one lump.

You can also label the parts: named groups written as (?<name>...) are supported, and the named values appear in each match's details. For a plain extraction run you rarely need them, but they help when debugging why a string matched. One ceiling to know: results stop at 500 matches, so a large text needs splitting into blocks, as the FAQs explain.

The pattern, piece by piece

The pattern [\w.+-]+@[\w-]+(\.[\w-]+)+ has four parts:

  • [\w.+-]+ is the local part, the bit before the @. \w covers letters, digits and underscore, and the class adds dot, plus and hyphen, so forms like ana.silva and j.okafor+work both fit. The + means one or more.
  • @ is a literal at sign.
  • [\w-]+ is the first domain label, letters, digits and hyphens.
  • (\.[\w-]+)+ is one or more dot-plus-label groups: .com is one group, .example.co.uk is three. Requiring at least one real dot here is what keeps fragments out.

The worked example below first uses a lazier pattern, \S+@\S+, which just asks for non-space characters around an @. It finds the real addresses, but it also accepts an address with no ending at all and glues trailing punctuation onto good matches. The refined pattern costs a few more characters and saves the cleanup.

How to extract email addresses, step by step

  1. Open the Regex Tester.
  2. Type the pattern, without slashes, in the Pattern field: [\w.+-]+@[\w-]+(\.[\w-]+)+
  3. Put g in the Flags field. This is the step that turns a one-match check into an extraction.
  4. Paste or type the text you want to search into the text box.
  5. Read the match list: each match shows its text, index and groups. Work down the list and take the addresses you need.

If the tester reports an invalid pattern, the usual causes are an unclosed bracket or parenthesis, a trailing backslash, or a quantifier with nothing to repeat. The tester shows a plain-language error rather than a stack trace.

Worked example: three addresses in a three-line text

Here is the test text, the kind of thing you might lift from a message thread:

Please send the invoice to [email protected] and copy [email protected].
The old address, help@acme-corp, is missing its ending, and webmaster@ is not complete.
Reach me at [email protected] after Friday.

Start loose, with \S+@\S+ and the g flag. The tester returns four matches: [email protected], then [email protected]. with the sentence's full stop glued on, then help@acme-corp, trailing comma and all, an address with no ending, and finally [email protected]. Even this loose pattern skips webmaster@, because there is nothing after the at sign for it to match.

Now run the refined pattern, [\w.+-]+@[\w-]+(\.[\w-]+)+, on the same text. The list drops to exactly three clean matches: [email protected] at index 27, [email protected] at index 58 with no full stop attached, and [email protected] at index 181. The fragment and the punctuation are gone, and nothing real was lost.

The dial turns the other way too. Replace the ending with a fixed \.com, giving [\w.+-]+@[\w-]+\.com, and the same text returns one match, [email protected]; the .org and .co.uk addresses are silently skipped. Extraction is a dial, not a switch, and the next section is about judging where your text needs it.

Tightening the pattern: too much, too little

Judge every version of the pattern by its match list, not by how clever the pattern looks. If the list contains things that are not addresses, find the feature they share and remove it: fragments with no dot are excluded by requiring the (\.[\w-]+)+ group, and punctuation glued to the end disappears when the final part of the pattern must be a label of word characters and hyphens rather than any non-space character.

If the list is missing addresses you can see in the text, compare one missed address against the pattern piece by piece. A missed plus sign means the local-part class is too narrow. Change one piece at a time and rerun, so you know which change fixed which miss. This loop beats guessing at a giant all-purpose pattern copied from somewhere else.

Limitations

Only extract addresses from text you have the right to use, and remember that a match proves only that a string is shaped like an address, not that the address exists.

The tester uses JavaScript's RegExp engine, so a pattern you tune here follows JavaScript rules, which differ slightly from the PCRE, Python and grep flavors; expect small adjustments when you reuse the pattern in code. Results stop at 500 matches per run. Finally, pathological patterns with nested quantifiers can make your browser tab busy on long text through catastrophic backtracking; if a run feels stuck, simplify the pattern rather than pasting more text.

Common mistakes

  • Forgetting the g flag. Without it, only the first match is reported, and an extraction that returns one address looks like a text that contains one address.
  • Typing slashes around the pattern. The Pattern field takes the pattern body only. Slashes belong to patterns written inside code, not to this field.
  • Trusting every match as a real address. The pattern certifies shape, nothing more. Delivery, ownership and consent are separate questions it cannot answer.
  • Stopping at the loosest pattern that returns something. \S+@\S+ returns results quickly and makes you clean them by hand. Two minutes of tightening is cheaper.
  • Expecting more than 500 results. Past the cap the list simply ends. Split large texts into blocks so the cap never hides matches from you.

FAQs

Why does the tester show only one email address? Without the g flag, only the first match is reported. Put g in the Flags field and the match list shows every match, up to the tester's limit of 500.

Do I type the pattern with slashes around it? No. Type just the pattern body, and put the flags in their own field. Slashes typed into the Pattern field become part of the pattern you are testing.

Why did an incomplete address appear in my results? A loose pattern such as \S+@\S+ accepts anything without spaces around the @, so fragments like help@acme-corp and trailing punctuation get caught. Requiring a dot and a final label, as the pattern in this guide does, removes most of them.

Can the tester check whether an address is real? No. It finds strings shaped like email addresses in the text you paste. It cannot prove that an address exists, belongs to anyone, or can receive mail.

What if my text contains more than 500 addresses? Results stop at 500 matches. Split the text into smaller blocks and run the pattern on each block, so no block can reach the cap without you noticing.

Conclusion

Extraction with regex is three habits: a pattern that describes the shape you want, the g flag so the Regex Tester lists every match instead of the first, and a tightening loop driven by reading the match list itself. The same position-and-anchor thinking that cleans up an extraction pattern is covered in the companion guide, Regex Anchors and Boundaries: ^, $ and \b Explained, and more tool walkthroughs live in the Developer Tools guides archive.