Strange ... I'm getting crazy

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-forum.com/.

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

-- fxn

···

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]

Hi --

···

On Thu, 21 Jun 2007, 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

--- Expected result

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

-----Actual result

emails_array => [teste@test.com]

I get:

["teste@test.com", " alf@test.com", " joe@teste.com"]

David

--
* Books:
   RAILS ROUTING (new! http://www.awprofessional.com/title/0321509242\)
   RUBY FOR RAILS (http://www.manning.com/black\)
* Ruby/Rails training
     & consulting: Ruby Power and Light, LLC (http://www.rubypal.com)

"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

···

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

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

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-forum.com/\.

Hi --

···

On Thu, 21 Jun 2007, dblack@wobblini.net wrote:

I get:

["teste@test.com", " alf@test.com", " joe@teste.com"]

No I don't; please ignore.

David

--
* Books:
   RAILS ROUTING (new! http://www.awprofessional.com/title/0321509242\)
   RUBY FOR RAILS (http://www.manning.com/black\)
* Ruby/Rails training
     & consulting: Ruby Power and Light, LLC (http://www.rubypal.com)