itsmetom has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I have this below issue in Perl.
I have a file in which I get list of emails as input.
I would like to parse the string before '@' of all email addresses. (Later I will store all the string before @ in an array)

For eg. in : abcdefgh@gmail.com, i would like to parse the email address and extract abcdefgh.
My intention is to get only the string before '@'.
Now the question is how to check it using regular expression. Or is there any other method using substr?
while I use regular expression : $mail =~ "\@" in Perl, it's not giving me the result.
Also, how will I find that the character '@' is in which index of the string $mail?
I appreciate if anyone can help me out.

#! usr/bin/perl
$mail = "abcdefgh@gmail.com";
if ($mail =~ "\@" ) {
print("my name = You got it!");
}
else
{
print("my name = Try again!");
}
even $mail =~ "\@" will not work.
$mail =~ "@" will work only if the given string $mail = "abcdefgh\@gmail.com";
But in my case, i will be getting the input with email address as its.
Not with an escape character.

Thanks,
Tom
  • Comment on RegEx to find the index of the character '@' in an Email address

Replies are listed 'Best First'.
Re: RegEx to find the index of the character '@' in an Email address
by Corion (Patriarch) on Jul 26, 2010 at 15:03 UTC

    Regular expressions are not strings, so don't use $mail =~ "...";. Maybe you want to just use index instead of a regular expression?

    If you really want to use a regular expression, don't use strings, use regular expressions:

    ... if( $mail =~ /\@/ ) { ... } ...
      Regular expressions are not strings
      Right, but numbers aren't strings either.

      Remember, this is Perl. If you use a string as a regexp, Perl will treat it as a regexp.

      my ($local_part) = $mail =~ '^([^@]*)';
      works just fine.

        Sure, but if you use double quoted strings, you forego the convenience of Perl knowing that you mean to write a single backslash when you want a backslash to appear in the regular expression.

Re: RegEx to find the index of the character '@' in an Email address
by Ratazong (Monsignor) on Jul 26, 2010 at 15:04 UTC
    $mail =~ /(.*?)\@/; my $first_part = $1; # better do some error-handling first ;-)

    But better have a look at CPAN for some nice email handling/parsing modules...

    HTH, Rata
Re: RegEx to find the index of the character '@' in an Email address
by almut (Canon) on Jul 26, 2010 at 15:25 UTC
    ...will work only if the given string $mail = "abcdefgh\@gmail.com"; But in my case, i will be getting the input with email address as its. Not with an escape character.

    The backslash is only needed to prevent the interpolation of an array @gmail in the double quoted string literal.  The actual value in $mail won't hold the backslash, just as the input values you're expecting to handle...

    That said, maybe have a look at Email::Address.

Re: RegEx to find the index of the character '@' in an Email address
by moritz (Cardinal) on Jul 26, 2010 at 16:33 UTC
    Or is there any other method using substr?

    Sure. You can use index to find the index of the first occurrence of @, and the use substr to extract everything before it.

    Perl 6 - links to (nearly) everything that is Perl 6.
Re: RegEx to find the index of the character '@' in an Email address
by NiJo (Friar) on Jul 26, 2010 at 17:06 UTC
Re: RegEx to find the index of the character '@' in an Email address
by ahmad (Hermit) on Jul 26, 2010 at 21:22 UTC

    There are more than one way to do it, not necessarily regex

    For example:

    my $str = 'MY Name@gmail.com'; my $name = substr $str, 0, index($str,'@'); # OR my ($name) = split '@', $str; # OR my ($name) = $str=~m/(.*?)\@/; print $name;

Re: RegEx to find the index of the character '@' in an Email address
by suhailck (Friar) on Jul 26, 2010 at 15:33 UTC
    perl -le '$email=q[abcdef@gmail.com];($username,$domainname)=$email=~/^(.*?)\@(.*?)$/;print $username,"\t",$domainname'

    output

    abcdef gmail.com

    ~suhail