We help IT Professionals succeed at work.

regex question

randor1973
randor1973 asked
on
Medium Priority
219 Views
Last Modified: 2010-03-05
ok,

i need to pull an email address from a line like so:

$read = 'hello and welcome to my place, my email address is me@myplace.com in case you want to write me';

now the problem is that the text/email will not be the same each time.. so far i got this:

$read =~ m/(\s.*?\@.*\..*\s)/i; but this pulls out everything from the first space to the one right after the email address..

can someone show me where i am going wrong on that??
Thanx

ps: sry for the points, but it seems that my points that i did have have dissapeared??
Comment
Watch Question

Here is a simple solution:

$read = 'hello and welcome to my place, my email address is me@myplace.com in case you want to write me';
$read =~ m/([^\s]*\@[^\s]*)/i;
print $1;

Author

Commented:
yeap, thank you so much..
Thanks.

Here is a a better solution that demands at least one dot in the address, and is not fooled by a comma or a period at the start of the address, or a comma at the end of the address.

$read =~ m/([^\s.,]*\@[^\s.,]+\.[^\s,]+)/i;

It is not perfect because, for example, it is fooled by a period at the end of an address.  It is up to you to add as much checking as is justified is your circumstances.

On the command line enter
    perldoc -q "How do I check a valid mail address?"
for more info

Explore More ContentExplore courses, solutions, and other research materials related to this topic.