Search code examples
rubyregexparsingblockindentation

Block Indent Regex


I'm having problems about a regexp.

I'm trying to implement a regex to select just the tab indent blocks, but i cant find a way of make it work:

Example:

INDENT(1)
    INDENT(2)
        CONTENT(a)
        CONTENT(b)
    INDENT(3)
        CONTENT(c)

So I need blocks like:

INDENT(2)
    CONTENT(a)
    CONTENT(b)

AND

INDENT(3)
    CONTENT(c)

How I can do this?


really tks, its almost that, here is my original need:

table
    tr
        td
            "joao"
            "joao"
        td
            "marcos"

I need separated "td" blocks, could i adapt your example to that?



Solution

  • It depends on exactly what you are trying to do, but maybe something like this:

    ^(\t+)(\S.*)\n(?:\1\t.*\n)*
    

    Working example: http://www.rubular.com/r/qj3WSWK9JR

    The pattern searches for:

    • ^(\t+)(\S.*)\n - a line that begins with a tab (I've also captured the first line in a group, just to see the effect), followed by
    • (?:\1\t.*\n)* - lines with more tabs.

    Similarly, you can use ^( +)(\S.*)\n(?:\1 .*\n)* for spaces (example). Mixing spaces and tabs may be a little problematic though.

    For the updated question, consider using ^(\t{2,})(\S.*)\n(?:\1\t.*\n)*, for at least 2 tabs at the beginning of the line.