# Html stringScanner regexp

**URL:** <https://www.ruby-forum.com/t/html-stringscanner-regexp/59546>\
**Category:** Ruby\
**Created:** [May 4, 2006, 1:12am UTC](https://www.ruby-forum.com/t/html-stringscanner-regexp/59546 "2006-05-04T01:12:42Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![tomas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/439d5e/32.png) [@tomas](https://www.ruby-forum.com/u/tomas)\
**Post date:** [May 4, 2006, 1:12am UTC](https://www.ruby-forum.com/t/html-stringscanner-regexp/59546/1 "2006-05-04T01:12:42Z")

</div>

Hi,

I want to parse a html string like this:

…

 title1 
...
 title2 
...
 title3 
...
 title4 
...

“…” means other stuff.  
I need to extract title1 to title4, so I tried

ScannerScan.scan(/._class=title\>(._)\</a\>\</div\>_NEWLINE_/) But I get  
only the last title – title4. Why? Is the regex wrong, or do I miss the  
point with the scan method?

Best regards  
Tomas

---

<div class="post-metadata">

**Author:** ![tomas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/439d5e/32.png) [@tomas](https://www.ruby-forum.com/u/tomas)\
**Post date:** [May 4, 2006, 1:34am UTC](https://www.ruby-forum.com/t/html-stringscanner-regexp/59546/2 "2006-05-04T01:34:59Z")

</div>

Tomas F. wrote:  
[…]

> ScannerScan.scan(/._class=title\>(._)\</a\>\</div\>_NEWLINE_/) But I get  
> only the last title – title4. Why? Is the regex wrong, or do I miss the  
> point with the scan method?

The .\* is greedy and gobbles up as much of the source as it can (up to  
the end of the string) and then the regex engine backtracks just enough  
to match the last occurance. You might try this instead:

%r{ class=title\>(.\*?)\n}

But remember that unless you can guarantee the formatting of your input  
won’t vary much you’re probably better off using a proper HTML parser to  
handle HTML rather than regexen.
