tags:

views:

60

answers:

2

How can I match a block of text that is indented using tabs?

Say I have the following text:

Lorem ipsum dolor sit amet, consectetur adipiscing elit. 

    # This is a text block
    @some = 'ruby'
    @then = 'some more'

Aliquam malesuada scelerisque orci, sed venenatis sem eleifend ac. Vestibulum vehicula sagittis commodo. Praesent dapibus lacinia aliquam.

I want to match all the content that is tabbed in (ie. the code).

A: 

As long as you're not using multiline matches, you can get away with this: /^\t+.+/.

Pinochle
+3  A: 

This will return an array with one string per block:

str = %Q{
Lorem ipsum dolor sit amet, consectetur adipiscing elit. 

\t# This is a text block
\t@some = 'ruby'
\t@then = 'some more'

Aliquam malesuada scelerisque orci, sed venenatis sem eleifend ac. Vestibulum vehicula sagittis commodo. Praesent dapibus lacinia aliquam.
\t# another block
\tfoo(bar)
}

str.scan(/(?:^\t.*\n)+/) #=> ["\t# This is a text block\n\t@some = 'ruby'\n\t@then = 'some more'\n", "\t# another block\n\tfoo(bar)\n"]
sepp2k
Exactly what I was after. Thanks a lot :D
JimNeath
This regex will falsely match parts of lines containing a tab, such as `%Q{ \tfoo}`. You need to anchor the tab to the start of lines, like this: `str.scan( /(?:^\t.+\n)+/ )`
FM
Good catch. Fixed.
sepp2k