如何匹配使用制表符缩进的文本块?
说我有以下文字:
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.
我希望匹配所有标签内容(即代码)。
答案 0 :(得分:3)
这将返回一个数组,每个块有一个字符串:
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"]
答案 1 :(得分:0)
只要你没有使用多行匹配,你就可以逃脱:/^\t+.+/
。