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.
J. m. (Guest)
on 2007-06-21 03:10
WTF:

    str = "removed_email_address@domain.invalid, 
removed_email_address@domain.invalid, removed_email_address@domain.invalid"

    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 => 
["removed_email_address@domain.invalid","removed_email_address@domain.invalid","removed_email_address@domain.invalid"]


-----Actual result

emails_array => [removed_email_address@domain.invalid]

Can't understand
Xavier N. (Guest)
on 2007-06-21 03:15
(Received via mailing list)
On Jun 21, 2007, at 1:10 AM, J. mp wrote:

>     str = "removed_email_address@domain.invalid, removed_email_address@domain.invalid, 
removed_email_address@domain.invalid"
>
>     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
J. m. (Guest)
on 2007-06-21 03:18
Xavier N. wrote:
> On Jun 21, 2007, at 1:10 AM, J. mp wrote:
>
>>     str = "removed_email_address@domain.invalid, removed_email_address@domain.invalid, 
removed_email_address@domain.invalid"
>>
>>     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
unknown (Guest)
on 2007-06-21 03:19
(Received via mailing list)
Hi --

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

>      if !tmp.nil?
>
>
> -----Actual result
>
> emails_array => [removed_email_address@domain.invalid]

I get:

["removed_email_address@domain.invalid", " 
removed_email_address@domain.invalid", " removed_email_address@domain.invalid"]


David
Marcel Molina Jr. (Guest)
on 2007-06-21 03: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

"removed_email_address@domain.invalid, removed_email_address@domain.invalid, 
removed_email_address@domain.invalid".split(',')
=> ["removed_email_address@domain.invalid", " 
removed_email_address@domain.invalid", " removed_email_address@domain.invalid"]

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:
"removed_email_address@domain.invalid, removed_email_address@domain.invalid, 
removed_email_address@domain.invalid".split(/\s*,\s*/)
=> ["removed_email_address@domain.invalid", 
"removed_email_address@domain.invalid", "removed_email_address@domain.invalid"]

marcel
unknown (Guest)
on 2007-06-21 03:22
(Received via mailing list)
Hi --

On Thu, 21 Jun 2007, removed_email_address@domain.invalid wrote:

> I get:
>
> ["removed_email_address@domain.invalid", " removed_email_address@domain.invalid", " 
removed_email_address@domain.invalid"]

No I don't; please ignore.


David
This topic is locked and can not be replied to.