Multiline regexp help

G

Gavin Kistner

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.
 
G

Gavin Kistner

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.)
 
P

Pit Capitain

Gavin said:
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
 
G

Gavin Kistner

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! :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top