Question Locked

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

0 helpful
3 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

I need to take an email address in this variable: $itemAuthorEmail
and break it apart into its components of:
$eMailID
$eMailDomain
$eMailSuffix

where
$itemAuthorEmail=user123@somewhere.net 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.

3 replies

by Member 25348569

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 user@domain.net.uk

by charles bundy

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!

This discussion is closed to new replies.