# Ruby multiline regex problem

**URL:** <https://www.ruby-forum.com/t/ruby-multiline-regex-problem/134188>\
**Category:** Ruby\
**Created:** [April 8, 2008, 6:21pm UTC](https://www.ruby-forum.com/t/ruby-multiline-regex-problem/134188 "2008-04-08T18:21:50Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![ransom](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ac8455/32.png) [@ransom](https://www.ruby-forum.com/u/ransom)\
**Post date:** [April 8, 2008, 6:21pm UTC](https://www.ruby-forum.com/t/ruby-multiline-regex-problem/134188/1 "2008-04-08T18:21:50Z")

</div>

Code:

“

testPit  
something here Best
”

Pattern:

\<td._?\>._?\</td\s\*\>

I’m trying to match this whole block and use it for further parsing.  
This started from an example in Brian Merick’s book “Everyday  
Scripting…” that had to be modified because amazon has changed their  
presentation to tables instead of lists.

Anyway, the regex works fine as a single-line. as soon as I introduce  
this:

"

testPit  
something here

Best

"

it fails.

When I try this same expression with perl using the //s mode, it works.  
I understand Ruby uses //m (multi-line mode in nearly the same fashion  
causing newlines to be considered any character, so it should work,  
right? Can anyone tell me what I am doing wrong here? Why isn’t  
“multiline” mode working?

Thanks!

---

<div class="post-metadata">

**Author:** ![ransom](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ac8455/32.png) [@ransom](https://www.ruby-forum.com/u/ransom)\
**Post date:** [April 8, 2008, 8:44pm UTC](https://www.ruby-forum.com/t/ruby-multiline-regex-problem/134188/2 "2008-04-08T20:44:07Z")

</div>

On Tue, Apr 8, 2008 at 11:21 AM, Gregg Y. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> href=“[http://www.amazon.com/Rails-Recipes/dp/0977616606/ref=pd\_sim\_b\_njs\_img\_1](http://www.amazon.com/Rails-Recipes/dp/0977616606/ref=pd_sim_b_njs_img_1)”\>testPit  
> “multiline” mode working?
> 
> Thanks!

`
s = '`
`testPit
something here
Best’
puts “######\ns:”
puts s
r1 = /<td.?>.?</td.?>/m
r2 = /<td.?>(.?)</td.?>/m
puts “######\nscan with r1:”
puts s.scan(r1)
puts
puts “######\nmatch with r1:”
puts (s.match r1)[0]
puts
s =~ r1
puts “######\n=~ and $1 with r1:”
puts $1
puts
puts
puts
puts “######\nscan with r2:”
puts s.scan(r2)
puts
puts “######\nmatch with r2:”
puts (s.match r2)[0]
puts
s =~ r2
puts “######\n=~ and $1 with r2:”
puts $1
`[ 

Hmm, I’m not sure if the regexp /\<td[^\>]_\>._?\</td[^\>]\*\>/m would be  
more appropriate or not.

 

Todd

](http://www.amazon.com/Rails-Recipes/dp/0977616606/ref=pd_sim_b_njs_img_1)

---

<div class="post-metadata">

**Author:** ![ransom](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ac8455/32.png) [@ransom](https://www.ruby-forum.com/u/ransom)\
**Post date:** [April 9, 2008, 3:18pm UTC](https://www.ruby-forum.com/t/ruby-multiline-regex-problem/134188/3 "2008-04-09T15:18:06Z")

</div>

2008/4/8, Gregg Y. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> href=“[Amazon.com](http://www.amazon.com/Rails-Recipes/dp/0977616606/ref=pd_sim_b_njs_img_1)”\>testPit  
> “multiline” mode working?  
> Works for me: no match without /m, match with /m:

irb(main):004:0\> s=%q{

\<a  
irb(main):005:0’  
href=“[Amazon.com](http://www.amazon.com/Rails-Recipes/dp/0977616606/ref=pd_sim_b_njs_img_1)”\>testPit  
irb(main):006:0’ something here Best}  
=\> “\<td align="left" \>\<div style="width: 165px; height:  
175px;"\>\<a\nhref="[http://www.amazon.com/Rails-Recipes/dp/09](http://www.amazon.com/Rails-Recipes/dp/09)  
77616606/ref=pd\_sim\_b\_njs\_img\_1"\>testPit\nsomething here Best”  
irb(main):007:0\> s[%r{\<td._?\</td\s_\>}]  
=\> nil  
irb(main):008:0\> s[%r{\<td._?\</td\s_\>}m]  
=\> “\<td align="left" \>\<div style="width: 165px; height:  
175px;"\>\<a\nhref="[http://www.amazon.com/Rails-Recipes/dp/09](http://www.amazon.com/Rails-Recipes/dp/09)  
77616606/ref=pd\_sim\_b\_njs\_img\_1"\>testPit\nsomething here Best”  
irb(main):009:0\>

Cheers

robert

---

<div class="post-metadata">

**Author:** ![ransom](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ac8455/32.png) [@ransom](https://www.ruby-forum.com/u/ransom)\
**Post date:** [April 10, 2008, 10:43am UTC](https://www.ruby-forum.com/t/ruby-multiline-regex-problem/134188/4 "2008-04-10T10:43:28Z")

</div>

2008/4/10, Ransom T. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> Thanks folks for all your help…turns out that I was using the regex  
> test view in Eclipse (RDT) which was obviously not behaving properly in  
> multi-line mode. I guess I need to go out and get the Aptana/Radrails  
> plugin that has the latest RDT and ruby-debug built in. I identified the  
> issue using Mike Lovitt’s Rubular regex tester. Thanks Mike for  
> restarting that server!

Why look so far? IRB serves the same purpose.

Cheers

robert

---

<div class="post-metadata">

**Author:** ![ransom](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ac8455/32.png) [@ransom](https://www.ruby-forum.com/u/ransom)\
**Post date:** [April 10, 2008, 4:52am UTC](https://www.ruby-forum.com/t/ruby-multiline-regex-problem/134188/5 "2008-04-10T04:52:43Z")

</div>

Thanks folks for all your help…turns out that I was using the regex  
test view in Eclipse (RDT) which was obviously not behaving properly in  
multi-line mode. I guess I need to go out and get the Aptana/Radrails  
plugin that has the latest RDT and ruby-debug built in. I identified the  
issue using Mike Lovitt’s Rubular regex tester. Thanks Mike for  
restarting that server!

> **[Rubular](https://rubular.com/)**
>
> Ruby-based regular expression editor/tester

---

<div class="post-metadata">

**Author:** ![ransom](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ac8455/32.png) [@ransom](https://www.ruby-forum.com/u/ransom)\
**Post date:** [April 10, 2008, 2:33pm UTC](https://www.ruby-forum.com/t/ruby-multiline-regex-problem/134188/6 "2008-04-10T14:33:52Z")

</div>

Robert K. wrote:

> 2008/4/10, Ransom T. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):
> 
> > Thanks folks for all your help…turns out that I was using the regex  
> > test view in Eclipse (RDT) which was obviously not behaving properly in  
> > multi-line mode. I guess I need to go out and get the Aptana/Radrails  
> > plugin that has the latest RDT and ruby-debug built in. I identified the  
> > issue using Mike Lovitt’s Rubular regex tester. Thanks Mike for  
> > restarting that server!
> 
> Why look so far? IRB serves the same purpose.
> 
> Cheers
> 
> robert

I’m a newb with Ruby and IRB. I did test the regex in IRB, but did not  
know that I could set a literal string up with \n characters like you  
did above through the interface. So, of course, it was passing  
everytime. That is very cool! I am growing fonder of IRB every day…
