Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Regex for e-mail contacts name

by lothar4ever (Initiate)
on Jan 23, 2012 at 19:42 UTC ( [id://949511]=perlquestion: print w/replies, xml ) Need Help??

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

I need a Regex that let me filter the name of the contacts that software like Outlook Express usually adds when sending emails.

Example:
I have this string:
$string = 'sean@gmail.com, "richard" <richard@gmail.com>, "john" <john@gmail.com>, <jack@gmail.com>';
And i want to have this:
'sean@gmail.com, <richard@gmail.com>, <john@gmail.com>, <jack@gmail.com>'

I know this is probably not hard, but don't know how to do it, tried everything i could think of.

Thanks.

PS: sorry for my english, is not my native language.

Replies are listed 'Best First'.
Re: Regex for e-mail contacts name
by i5513 (Pilgrim) on Jan 23, 2012 at 21:20 UTC
Re: Regex for e-mail contacts name
by JavaFan (Canon) on Jan 23, 2012 at 22:21 UTC
    For your given examples, it seems that /\S+@\S+/ will do.
Re: Regex for e-mail contacts name
by flexvault (Monsignor) on Jan 23, 2012 at 19:55 UTC

    lothar4ever,

    Why not show us something you tried?

    You'll get a lot more help, if we have something to help analyze with you.

    Just make sure to use <code> ... </code>.

    "Well done is better than well said." - Benjamin Franklin

      Things i've tried:
      s/(?<=")[^"><]*(?=")//g
      s/\".+?\<//g
      There is a few more, but i don't remember them, there was so many tries, this where the last 2.

        lothar4ever,

        You don't say where the data is coming from, so to get your $string to be what you want, you need to escape the '@' signs:

        my $string = qq|sean\@gmail.com, "richard" <richard\@gmail.com>, "john +" <john\@gmail.com>, <jack\@gmail.com>|;

        For this, I would use 'split': ( tested code, but re-typed from X-Terminal ):

        my @email = split(/\,/,$string); for my $var( 0 .. $#email ) { print "$email[$var]\n"; ..... # put additional code here }

        Now you have an array that you could use a regex or 'split' again on each element of the array in get the format you want. If the data is coming from disk, it maybe that each email address is on a line terminated with a 'CR'.

        Hope this helps.

        "Well done is better than well said." - Benjamin Franklin

Re: Regex for e-mail contacts name
by sundialsvc4 (Abbot) on Jan 24, 2012 at 05:21 UTC

    I suggest that you spend some time looking at the various Regexp::Common entries ...

    Note:   Follow the search-link on that page and you will see that there are (currently...) 139 CPAN packages that fall under that category.   It is quite likely that the regex you are looking for has already been done, or that you will find something that can easily be adapted.

Re: Regex for e-mail contacts name
by Marshall (Canon) on Jan 24, 2012 at 18:55 UTC
    Instead of trying to find the e-mail addresses, another approach is to take out the stuff that isn't one. Looks like the stuff between double quotes.

    The regex matches any word character, ".", "," or space within double quotes and deletes that.

    #!/usr/bin/perl -w use strict; my $string = 'sean@gmail.com, "richard J. Smith, M.D." <richard@gmail. +com>, "john" <john@gmail.com>, <jack@gmail.com>'; $string =~ s/\"[\w .,]+"//g; print $string; #sean@gmail.com, <richard@gmail.com>, <john@gmail.com>, <jack@gmail. +com>

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://949511]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (2)
As of 2024-04-26 00:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found