Regex problem

hi @all

I would like to scan a string of html-tags. I need it to take out all
links (a-tags) in the string, but I become only the last one. What is
wrong? See the code below...

response = '<a href="hello1.html">test1</a> - <a
href="hello2.html">test2</a>'
response.scan(/<a.*href="(.*?)"/) do |line|
  puts line
end

thanks for helping!

···

--
Posted via http://www.ruby-forum.com/.

the first kleene star might need to be non greedy? in other words stop
at the first href consumed, not the last.
/<a.*?href="(.*?)"/

···

On Nov 27, 11:28 am, "K. R." <m...@palstek.ch> wrote:

hi @all

I would like to scan a string of html-tags. I need it to take out all
links (a-tags) in the string, but I become only the last one. What is
wrong? See the code below...

response = '<a href="hello1.html">test1</a> - <a
href="hello2.html">test2</a>'
response.scan(/<a.*href="(.*?)"/) do |line|
  puts line
end

thanks for helping!
--
Posted viahttp://www.ruby-forum.com/.

Franco is right. You could fix it by doing "a.*?href". However, I
would change "a.*href" to "a\s+href" since you're looking for any
amount of whitespace after the "a" and before the "href".

response = '<a href="hello1.html">test1</a> - <a href="hello2.html">test2</a>'
response.scan(/<a\s+href="(.*?)"/s) do |line|
puts line
end

···

On Nov 27, 2007 11:28 AM, K. R. <mcse@palstek.ch> wrote:

hi @all

I would like to scan a string of html-tags. I need it to take out all
links (a-tags) in the string, but I become only the last one. What is
wrong? See the code below...

response = '<a href="hello1.html">test1</a> - <a
href="hello2.html">test2</a>'
response.scan(/<a.*href="(.*?)"/) do |line|
  puts line
end

thanks for helping!
--
Posted via http://www.ruby-forum.com/\.

> hi @all

> I would like to scan a string of html-tags. I need it to take out all
> links (a-tags) in the string, but I become only the last one. What is
> wrong? See the code below...

> response = '<a href="hello1.html">test1</a> - <a
> href="hello2.html">test2</a>'
> response.scan(/<a.*href="(.*?)"/) do |line|
> puts line
> end

but what if href is not the first attribute of <a/>?

···

On Nov 27, 12:00 pm, Christian von Kleist <cvonkle...@gmail.com> wrote:

On Nov 27, 2007 11:28 AM, K. R. <m...@palstek.ch> wrote:

> thanks for helping!
> --
> Posted viahttp://www.ruby-forum.com/.

Franco is right. You could fix it by doing "a.*?href". However, I
would change "a.*href" to "a\s+href" since you're looking for any
amount of whitespace after the "a" and before the "href".

response = '<a href="hello1.html">test1</a> - <a href="hello2.html">test2</a>'
response.scan(/<a\s+href="(.*?)"/s) do |line|
puts line
end

response.scan(/<a.*href="(.*?)"/) do |line|

but what if href is not the first attribute of <a/>?

Regardless which order has the attributes, because you can have any
sequence (.*) between the <a tag and href.

···

--
Posted via http://www.ruby-forum.com/\.