views:

127

answers:

6

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 : [email protected], 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 = "[email protected]";

if ($mail =~ "\@" ) {
    print("my name = You got it!");
}
else
{
    print("my name = Try again!");
}

In the above code $mail =~ "\@" is not giving me desired output but ($mail =~ "abc" ) does.

$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

A: 

What if you tried this:

my $email = '[email protected]';

$email =~ /^(.+?)@/;
print $1

$1 will be everything before the @.

Matthew J Morrison
+2  A: 

The @ sign is a metacharacter in double-quoted strings. If you put your email address in single quotes, you won't get that problem.

Also, I should add the obligatory comment that this is fine if you're just experimenting, but in production code you should not parse email addresses using regular expressions, but instead use a module such as Mail::Address.

Dan
[Email::Address](http://p3rl.org/Email::Address) is better.
daxim
A: 

If you already know it's a vaild email, you can just use substr and index:

$email = '[email protected]';
print substr($email, 0, index($email, '@'))

Since there should be only one '@' in the email.

NullUserException
rfc2822 allows for '@' in the local-part (everything before the last '@') as long as it is in a quoted string. In theory an address like "Not@home"@example.com is valid, in practice enough people don't know this, and as a result write broken code that can't handle it, that your likely not to get much mail to an address like that.
Ven'Tatsu
+1  A: 

If you want the index of a string, you can use the index() function. ie.

my $email = 'foo@bar';
my $index = index($email, '@');

If you want to return the former half of the email, I'd use split() over regular expressions.

my $email = 'foo@bar';
my @result = split '@', $email;
my $username = $result[0];

Or even better with substr

my $username = substr($email, 0, index($email, '@'))
injekt
You should use rindex in this code to look for the last @ not the first. '@' is valid in the local-part (`$username`) as a part of a quoted string according to rfc2822.
Ven'Tatsu
A: 
$mail = '[email protected]';
$mail =~ /^([^@]*)@/;
print "$1\n"
Zac Thompson
+6  A: 

Enabling warnings would have pointed out your problem:

#!/usr/bin/perl
use warnings;

$mail = "[email protected]";
__END__
Possible unintended interpolation of @gmail in string at - line 3.
Name "main::gmail" used only once: possible typo at - line 3.

and enabling strict would have prevented it from even compiling:

#!/usr/bin/perl
use strict;
use warnings;

my $mail = "[email protected]";
__END__
Possible unintended interpolation of @gmail in string at - line 4.
Global symbol "@gmail" requires explicit package name at - line 4.
Execution of - aborted due to compilation errors.

In other words, your problem wasn't the regex working or not working, it was that the string you were matching against contained "abcdefgh.com", not what you expected.

ysth