Strange ... I'm getting crazy

WTF:

str = "[email protected], [email protected], [email protected]"

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 => [“[email protected]”,“[email protected]”,“[email protected]”]

-----Actual result

emails_array => [[email protected]]

Can’t understand

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

str = "[email protected], [email protected], [email protected]"

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

Hi –

On Thu, 21 Jun 2007, J. mp wrote:

 if !tmp.nil?

-----Actual result

emails_array => [[email protected]]

I get:

[“[email protected]”, " [email protected]", " [email protected]"]

David

On Thu, Jun 21, 2007 at 08:10:33AM +0900, J. mp wrote:

  if !tmp.nil?
    emails_array << tmp

  end
 end

[email protected], [email protected], [email protected]”.split(’,’)
=> [“[email protected]”, " [email protected]", " [email protected]"]

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:
[email protected], [email protected], [email protected]”.split(/\s*,\s*/)
=> [“[email protected]”, “[email protected]”, “[email protected]”]

marcel

Hi –

On Thu, 21 Jun 2007, [email protected] wrote:

I get:

[“[email protected]”, " [email protected]", " [email protected]"]

No I don’t; please ignore.

David

Xavier N. wrote:

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

str = "[email protected], [email protected], [email protected]"

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