[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

strange ... I'm getting crazy

J. mp

6/20/2007 11:11:00 PM

WTF:

str = "teste@test.com, alf@test.com, joe@teste.com"

emails_array = Array.new
emails = str.split(",")
emails.each do |single_str|

tmp = single_str[/^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i]

if !tmp.nil?
emails_array << tmp

end
end



--- Expected result

emails_array => ["teste@test.com","alf@test.com","joe@teste.com"]


-----Actual result

emails_array => [teste@test.com]

Can't understand

--
Posted via http://www.ruby-....

5 Answers

Xavier Noria

6/20/2007 11:16:00 PM

0

On Jun 21, 2007, at 1:10 AM, J. mp wrote:

> str = "teste@test.com, alf@test.com, joe@teste.com"
>
> emails_array = Array.new
> emails = str.split(",")
> emails.each do |single_str|
>
> tmp = single_str[/^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i]

There's a space after each comma in str that the regexp does not
allow. Either remove them or split by /\s*,\s*/.

-- fxn




dblack

6/20/2007 11:17:00 PM

0

Marcel Molina Jr.

6/20/2007 11:18:00 PM

0

On Thu, Jun 21, 2007 at 08:10:33AM +0900, J. mp wrote:
> WTF:
>
> str = "teste@test.com, alf@test.com, joe@teste.com"
>
> emails_array = Array.new
> emails = str.split(",")
> emails.each do |single_str|
>
> tmp = single_str[/^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i]
>
> if !tmp.nil?
> emails_array << tmp
>
> end
> end

"teste@test.com, alf@test.com, joe@teste.com".split(',')
=> ["teste@test.com", " alf@test.com", " joe@teste.com"]

Notice the white space at the start of all but the first email address.

With that whitespace there, the /^ in your regex won't be satisfied.

You probably want do split more like this:
"teste@test.com, alf@test.com, joe@teste.com".split(/\s*,\s*/)
=> ["teste@test.com", "alf@test.com", "joe@teste.com"]

marcel
--
Marcel Molina Jr. <marcel@vernix.org>

J. mp

6/20/2007 11:18:00 PM

0

Xavier Noria wrote:
> On Jun 21, 2007, at 1:10 AM, J. mp wrote:
>
>> str = "teste@test.com, alf@test.com, joe@teste.com"
>>
>> emails_array = Array.new
>> emails = str.split(",")
>> emails.each do |single_str|
>>
>> tmp = single_str[/^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i]
>
> There's a space after each comma in str that the regexp does not
> allow. Either remove them or split by /\s*,\s*/.
>
> -- fxn

God bless you. you're right I'm stupid
thanks

--
Posted via http://www.ruby-....

dblack

6/20/2007 11:21:00 PM

0