[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Multiline regexp help

Gavin Kistner

6/22/2005 1:21:00 PM

I thought that all four of the following puts statements should be
the same (except for the leading indentation). My intent was for
match_indented to match either case. I particularly don't understand
why the first puts fails.

Do I need more coffee? (Or rather, do I need to start drinking coffee?)


flat = <<ENDSTR
Hello
{{{
Sweet contents.
}}}
World
ENDSTR

indented = <<ENDSTR
Hello
{{{
Sweet contents.
}}}
World
ENDSTR

match_block = /^\{\{\{\n(.+?)\n\}\}\}\n/m
match_indented = /^( |\t)*\{\{\{\n(.+?)\n\1\}\}\}\n/m

puts flat[ match_block, 2 ]
puts flat[ match_indented, 2 ]
puts indented[ match_block, 2 ]
puts indented[ match_indented, 2 ]

#=> nil
#=> nil
#=> nil
#=> Sweet contents.



3 Answers

Gavin Kistner

6/22/2005 1:38:00 PM

0

On Jun 22, 2005, at 7:21 AM, Gavin Kistner wrote:
> match_indented = /^( |\t)*\{\{\{\n(.+?)\n\1\}\}\}\n/m

Oops. That should have been:
match_indented = /^(( |\t)*)\{\{\{\n(.+?)\n\1\}\}\}\n/m

which makes it work for the general case and solves my problem. I
still don't understand why the match_block regexp failed on the first
case, though.

(And I'm on crack. Of COURSE "match_block" would never have matched
the indented content.)


Pit Capitain

6/22/2005 2:02:00 PM

0

Gavin Kistner schrieb:
> Do I need more coffee? (Or rather, do I need to start drinking coffee?)

I don't think you have to. All you need is a pair as in pair programming.

> match_block = /^\{\{\{\n(.+?)\n\}\}\}\n/m
> match_indented = /^( |\t)*\{\{\{\n(.+?)\n\1\}\}\}\n/m
>
> puts flat[ match_block, 2 ]
> puts flat[ match_indented, 2 ]
> puts indented[ match_block, 2 ]
> puts indented[ match_indented, 2 ]

Since match_block only has one group, the second parameter should be 1
for match_block as in

xxx[ match_block, 1 ]

HTH

Pit


Gavin Kistner

6/22/2005 3:29:00 PM

0

On Jun 22, 2005, at 8:02 AM, Pit Capitain wrote:
>> Do I need more coffee? (Or rather, do I need to start drinking
>> coffee?)
>
> I don't think you have to. All you need is a pair as in pair
> programming.

Heh, that's the ticket.

> Since match_block only has one group, the second parameter should
> be 1 for match_block as in
> xxx[ match_block, 1 ]

Bah, thanks! :)