Forum: Ruby strange ... I'm getting crazy

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
A00673345921ae8c2e5570d1bd48b2e2?d=identicon&s=25 J. mp (lerias)
on 2007-06-21 01:10
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
7223c62b7310e164eb79c740188abbda?d=identicon&s=25 Xavier Noria (Guest)
on 2007-06-21 01:15
(Received via mailing list)
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
A00673345921ae8c2e5570d1bd48b2e2?d=identicon&s=25 J. mp (lerias)
on 2007-06-21 01:18
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
1fba4539b6cafe2e60a2916fa184fc2f?d=identicon&s=25 unknown (Guest)
on 2007-06-21 01:19
(Received via mailing list)
Hi --

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

>      if !tmp.nil?
>
>
> -----Actual result
>
> emails_array => [teste@test.com]

I get:

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


David
Cee0292fffa691f1fb320d5400200e99?d=identicon&s=25 Marcel Molina Jr. (Guest)
on 2007-06-21 01:20
(Received via mailing list)
On Thu, Jun 21, 2007 at 08:10:33AM +0900, J. mp wrote:
>       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
1fba4539b6cafe2e60a2916fa184fc2f?d=identicon&s=25 unknown (Guest)
on 2007-06-21 01:22
(Received via mailing list)
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
This topic is locked and can not be replied to.