Custom pattern to match e-mail addresses anyone?

Get help. Get answers. Let others lend you a hand.

Moderator: Mr_Noodle

I have a need to extract e-mail addresses (From and To) from e-mails saved as searchable PDFs. Currently I use a custom shell script which is pretty reliable but ideally I'd like to be able to do this using Hazel's built-in pattern matching. Now, this is a non-trivial problem to be sure; I was wondering if anyone has solved this and is willing to share their custom pattern?

Maybe there is a case for a specific 'email address' pattern to be implemented?
ChrisJenkins
 
Posts: 8
Joined: Sun Jan 28, 2018 11:44 am

It depends on how "strict" you want to be about email addresses. If these are emails, though, you could, for instance, just grab any text that appears after the "From:" header, maybe looking for the characters before and after the "@".
Mr_Noodle
Site Admin
 
Posts: 11255
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City

I was trying to come up with a pattern that would do that but it is surprisingly difficult as there is a lot of variation in the text after From: and To:

abc@xyz.com
abc-def@xyz.com
abc-def+tag@ghi.xyz.com
Fred Bloggs fred@somedomain.com
<abc@xyz.com>
Jane Doe <jane@doe.com>

If one wants to be sure of getting just the actual address and not any of the extra junk it's quite hard using the current capabilities in Hazel (unless I am missing a trick or two, which is quite possible). I have a custom shell script that uses things like sed, awk etc. and which does a pretty reasonable job but it would be nice to move this processing into Hazel rules at some point.

Of course none of this deals with the fact that nowadays you can have almost any national character in an e-mail address, especially if quoted, so if one wants to handle any possible address it is a big challenge. Luckily, my requirements are not quite that stringent.
ChrisJenkins
 
Posts: 8
Joined: Sun Jan 28, 2018 11:44 am

You can try a pattern like (any characters)@(any characters). You might need to add extra patterns to exclude the angle brackets but I don't think doing this all in one pattern is feasible.
Mr_Noodle
Site Admin
 
Posts: 11255
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City


Return to Support

cron