scan string w regex .. and HNYTAOY !

J

Josselin

HNYTAOY (Dry version of ... Happy New Year To All Of You)

I am trying to get an array of email addresses from a string , as this
string is coming from an input textarea in a from, even if I ask for a
blank between each address, users will also enter some return
characters...

so I get something like :

to_string =
"(e-mail address removed)\r\[email protected]\r\[email protected]
(e-mail address removed)"

I know how to use a_string.scan(" "), but here I should use a regex..
after many try .. could not find the correct one..
could someone make 2007 starting on right tracks.... thanks a lot !!

joss
 
J

Josselin

HNYTAOY (Dry version of ... Happy New Year To All Of You)

I am trying to get an array of email addresses from a string , as this
string is coming from an input textarea in a from, even if I ask for a
blank between each address, users will also enter some return
characters...

so I get something like :

to_string =
"(e-mail address removed)\r\[email protected]\r\[email protected]
(e-mail address removed)"

I know how to use a_string.scan(" "), but here I should use a regex..
after many try .. could not find the correct one..
could someone make 2007 starting on right tracks.... thanks a lot !!

joss

forgot to mention what I wwrote until now :
 
J

Josselin

forgot to mention what I wwrote until now :


I would just scan for email addresses. I got this:
to_string.scan(/[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}/i)

From this site:
http://www.regular-expressions.info/email.html

Which seems to do the job reasonably well.
Happy new year to you too!
-Mat

thanks Mat... I got it too.. my concern is getting all adresses in one array...
It seems that I could do a scan first to replace the \r\n by a space
then doing a split on space character will give me the array
finally I'll use the regex to chexk each email address ( I am using a
Rails plugin validates_as_email , based on RFC2822 with a possibility
to perform an online checking if needed)

Joss
 
R

Robert Klemme

I would just scan for email addresses.

Yeah, that's what I'd also prefer over splitting.
I got this:
to_string.scan(/[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}/i)

From this site:
http://www.regular-expressions.info/email.html

Which seems to do the job reasonably well.

Hm, IMHO the domain part could be a bit better with respect to dots.
Something like

(?:[A-Z0-9-]+\.)+[A-Z]{2,4}

In some circumstances something simple like this will even work

[^\s@]+@[^\s@]+

And I bet there is a ton of variants...

Cheers

robert
 
J

James Edward Gray II

forgot to mention what I wwrote until now :

You can split() on any whitespace characters with:

to_string.split

That seems easier than a regex to match an email.

James Edward Gray II
 
M

Mat Schaffer

You can split() on any whitespace characters with:

to_string.split

That seems easier than a regex to match an email.

/me slaps his forehead

agreed....
-Mat
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top