Email address parsing

From PostgreSQL wiki
Revision as of 09:03, 25 March 2009 by Petere (talk | contribs) (New page: {{SnippetInfo|Function to parse email addresses|depends=Email::Address|lang=PL/Perl}} This function extracts the real name from an email address. With slight variations, other components ...)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Snippets

Function to parse email addresses

Works with PostgreSQL

Any version

Written in

PL/Perl

Depends on

Email::Address

This function extracts the real name from an email address. With slight variations, other components such as local part or host name can be extracted. Relying on a well-tested Perl module makes this function extremely robust, compared to say handcrafted pattern-matching approaches.

CREATE OR REPLACE FUNCTION email_name(email text) RETURNS text
LANGUAGE plperlu
AS $$
use Email::Address;

my @addresses = Email::Address->parse($_[0]);
return undef unless scalar(@addresses) > 0;
return $addresses[0]->name;
$$;