# Regex problem

**URL:** <https://www.ruby-forum.com/t/regex-problem/121402>\
**Category:** Ruby\
**Created:** [November 27, 2007, 5:28pm UTC](https://www.ruby-forum.com/t/regex-problem/121402 "2007-11-27T17:28:11Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![flnative](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/5daacb/32.png) [@flnative](https://www.ruby-forum.com/u/flnative)\
**Post date:** [November 27, 2007, 5:28pm UTC](https://www.ruby-forum.com/t/regex-problem/121402/1 "2007-11-27T17:28:11Z")

</div>

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 = ‘test1 - test2’  
response.scan(/\<a._href="(._?)"/) do |line|  
puts line  
end

thanks for helping!

---

<div class="post-metadata">

**Author:** ![flnative](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/5daacb/32.png) [@flnative](https://www.ruby-forum.com/u/flnative)\
**Post date:** [November 27, 2007, 5:45pm UTC](https://www.ruby-forum.com/t/regex-problem/121402/2 "2007-11-27T17:45:41Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![flnative](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/5daacb/32.png) [@flnative](https://www.ruby-forum.com/u/flnative)\
**Post date:** [November 27, 2007, 6:01pm UTC](https://www.ruby-forum.com/t/regex-problem/121402/3 "2007-11-27T18:01:31Z")

</div>

On Nov 27, 2007 11:28 AM, K. R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> end
> 
> ## thanks for helping!
> 
> Posted via [http://www.ruby-forum.com/](http://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 = ‘test1 - test2’  
response.scan(/\<a\s+href=“(.\*?)”/s) do |line|  
puts line  
end

---

<div class="post-metadata">

**Author:** ![flnative](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/5daacb/32.png) [@flnative](https://www.ruby-forum.com/u/flnative)\
**Post date:** [December 1, 2007, 5:35pm UTC](https://www.ruby-forum.com/t/regex-problem/121402/4 "2007-12-01T17:35:22Z")

</div>

On Nov 27, 12:00 pm, Christian von Kleist [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> > response = ‘test1 - test2’  
> > response.scan(/\<a._href="(._?)"/) do |line|  
> > puts line  
> > end  
> > but what if href is not the first attribute of ?

---

<div class="post-metadata">

**Author:** ![flnative](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/5daacb/32.png) [@flnative](https://www.ruby-forum.com/u/flnative)\
**Post date:** [December 2, 2007, 2:05pm UTC](https://www.ruby-forum.com/t/regex-problem/121402/5 "2007-12-02T14:05:43Z")

</div>

> > response.scan(/\<a._href="(._?)"/) do |line|  
> > but what if href is not the first attribute of ?

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