How Convert Email to ID@Domain.Suffix using Perl?

By junktl ·
I need to take an email address in this variable: $itemAuthorEmail
and break it apart into its components of:

$ becomes:
$eMailID = user123
$eMailDomain = somewhere
$eMailSuffix = net

I am thinking this is easy, but I have having difficulty with figuring out how to write the code.

This conversation is currently closed to new comments.

Thread display: Collapse - | Expand +

All Answers

Collapse -

It is easy - Well, it was 'til I read it properly!

by neilb@uk In reply to How Convert Email to ID@D ...

One simple way is to use the the .split() method of the text string which will separate the string into an array. Clunky, but easy to implement.

$eMailID = $ItemAuthorEmail.split("@")[0]
$eMailDomain = $ItemAuthorEmail.split("@")[1].split(".")[0]
$eMailSuffix = $ItemAuthorEmail.split(".")[1]

Neil :)

Ooops, didn't notice it was Perl! Saw the $variables and immediately read it as PowerShell. Still might work if there's a split method but I have absolutely no idea if there is!

Falls over, anyway with a

Collapse -

E-Mail suffix/prefix is pretty easy

by Charles Bundy In reply to How Convert Email to ID@D ...

Try -

$position = rindex($itemAuthorEmail, ".") + 1;
$eMailSuffix = substr($itemAuthorEmail, $position);

With regards to eMailDomain you would have to handle cases of one or more ".". Use index() rather than rindex() for eMailID.

Also very important to check that $itemAuthorEmail contains a valid e-mail address!

Related Discussions

Related Forums