Extract all email adresses from text

Good day!

I am looking for a solution for filtering and extracting only email addresses from a text using regular expression and copy the email addresses to a variable, tried a lot but without success.

I'd suggest doing a 'net search for "regex email". Here is one result:
Email Address Regular Expression That 99.99% Works

1 Like

I have already tried that, KM search using regex can only find one email, not sure how to fix it. Any ideas?

Yep, the Search returns only the first match. To process all matches, you need to use For Each action with a Collection of substrings matching in.

See the last link for a good example.

1 Like

Thank you, will work on it!

Hey Kirill,

The method JM is suggesting will work just fine, but there are alternatives on the forum:

How to find all e-mail addresses in string/variable via regex?


1 Like

Oh, yeah...

For this task I favor this regular expression myself.

How to Find or Validate an Email Address:



1 Like

The post by @ComplexPoint does the trick.

@JMichaelTX's option also works :+1:
@ccstone Your script works perfectly, also i noticed that it can remove duplicates!

Guys, thanks a lot for the help!

One more thing - How can i count founded emails in a variable?
@ccstone @JMichaelTX

Just put a KM Variable to count the emails in the For Each loop:
Set Variable Calculation Local__NumEmails to Local__NumEmails + 1

and, of course, initialize it just before the loop:
Set Variable Calculation Local__NumEmails to 0

See Set Variable to Calculation action.

Could you please share a macro for this?

I didn't quite understand how to build such macro

Already done. See:
MACRO: Get List of RegEx Capture Group of Multiple Matches

For more info, see the KM Wiki:
Example #2: Extract Capture Group for Multiple Matches to Multiple Lines in a Variable

This shows you how to count the number of matches:



Need a detailed explanation of this macro as it doesn't work or i am doing something wrong. :confused:

[JS] Get List of JavaScript Functions in Selection.kmmacros (24.0 KB)

Looks like there is a mistake in regex as KM can't find any matches

Please take note of the name of the Macro example I gave you:
"[JS] Get List of JavaScript Functions in Selection.kmmacros"

Clearly, this has nothing to do with emails. It is just an example of how to use For Each to loop through all matches found in the source text.

Don't try to run the example macro. Just look at it, and use it as a pattern to build your own macro.

For the RegEx, you can use either what I posted above, or what @ccstone used in his macro.

Try to build your own macro from the info provided. If you can't get it working. post your macro and we'll try to help.


Thank you for the guide.

I am not so well educated in such things and it's getting harder and harder for me, but i keep trying to learn and doing some dumb mistakes, so please forgive me :sneezing_face:

Here is the macro Multiple Matches.kmmacros (4.7 KB)

The variable "EmailsToPaste" contains 2 emails, each on a new line

I still can't get it how to use "For Each" correctly to get number of contents, please help.

I'm afraid I don't have time to offer a detailed explanation, but here's a version of your macro that does what you want:

Multiple Matches.kmmacros (4.4 KB)



Try studying this to see how it works, perhaps going through it one step at a time with the debugger if you need to. Good luck!



Thanks a lot, no more questions :+1:

Thank you for this! Helped me enormously. Fast too!

1 Like