[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 320
  • Last Modified:

RegExp - Matching email addresses that could be well mixed into other text

Hi All,

I have some VBA code that I am using to find email addresses in email bodies in Outlook (the full code can be found in another question linked here, but you probably only need to know what is posted in this question - I am referencing for completeness and context).

The code is now working as expected, but I need to work on the RegEx.

This is what I am using currently:

MyPattern = "\b\w*@\w*\.\w*\.\w*\b|\b\w*@\w*\.\w*\b"

It was pointed out in the other thread that this would match:

alan@example.com
alan@example.co.nz
alan_3285@example.com

but would also match:

@.
1@2.3.

and would not match:

alan.3285@example.com
alan_o'leary@example.com
alan-3285@example.com


So, I need to try and find an expression that will solve those cases too.

Thanks,

Alan.
0
Alan
Asked:
Alan
1 Solution
 
Terry WoodsIT GuruCommented:
If you'd like to go into great detail about what to try to match, there's a great article on the topic here: http://www.regular-expressions.info/email.html

The recommended email matching pattern from that article, which is intended to keep things simple, is:
\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b

Open in new window

(it's assumed an ignore-case option is turned on, so lower case  characters can also be matched)

That pattern would match all your examples, except for the one with the single quote. To allow that too, it would need to be adjusted to:
\b[A-Z0-9._%+'-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b

Open in new window


If you view the more complicated (and strictly more correct) patterns in the article, you'll understand why keeping things simple is desirable!
0
 
AlanConsultantAuthor Commented:
Looks good to me!

Thank you,

Alan.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now