strange ... I'm getting crazy

J

J. mp

WTF:

str = "(e-mail address removed), (e-mail address removed), (e-mail address removed)"

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 => ["(e-mail address removed)","(e-mail address removed)","(e-mail address removed)"]


-----Actual result

emails_array => [[email protected]]

Can't understand
 
X

Xavier Noria

str = "(e-mail address removed), (e-mail address removed), (e-mail address removed)"

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
 
D

dblack

Hi --

WTF:

str = "(e-mail address removed), (e-mail address removed), (e-mail address removed)"

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 => ["(e-mail address removed)","(e-mail address removed)","(e-mail address removed)"]


-----Actual result

emails_array => [[email protected]]

I get:

["(e-mail address removed)", " (e-mail address removed)", " (e-mail address removed)"]


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)
 
M

Marcel Molina Jr.

WTF:

str = "(e-mail address removed), (e-mail address removed), (e-mail address removed)"

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

"(e-mail address removed), (e-mail address removed), (e-mail address removed)".split(',')
=> ["(e-mail address removed)", " (e-mail address removed)", " (e-mail address removed)"]

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:
"(e-mail address removed), (e-mail address removed), (e-mail address removed)".split(/\s*,\s*/)
=> ["(e-mail address removed)", "(e-mail address removed)", "(e-mail address removed)"]

marcel
 
J

J. mp

Xavier said:
str = "(e-mail address removed), (e-mail address removed), (e-mail address removed)"

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,262
Messages
2,571,310
Members
47,978
Latest member
SheriBolli

Latest Threads

Top