delphi – Extract emails from a Memo

Question:

I have a memo component, inside it there is a large text, and in that text it contains several e-mail addresses scattered around.

How can I extract only the emails from this memo1 and play in memo2 ?

Answer:

The following routine would work in Python, you could try converting to use Delphi's regular expression class. Remembering that there is no regular expression that takes 100% of valid emails, but good expressions take 99.9…%.

import re
import sys

email_regex = "[\w\.\+\-]+\@[\w]+\.[a-z]{2,3}"
regex = re.compile(email_regex)

data = open(sys.argv[1]).read()

for email in regex.finditer(data):
    print data[email.start(0):email.end(0)]
Scroll to Top