Ruby 正则表达式匹配元素在匹配之前离开

Ruby 正则表达式匹配元素在匹配之前离开,ruby,regex,match,Ruby,Regex,Match,我得到了一个具有以下结构的文件: [section1] item1 = text1 item2 = text2 item3 = text3 [section2] item1 = text4 item2 = text5 item3 = text6 ... 我会找到一段文字。e、 g.文本为“text6”,应返回“section2” 我打成平局: content.scan(/(^\[.*?\]).*?#{text}/m) 但这只是返回文件的第一部分:/ (我使用ruby) 这应该有效:) /

我得到了一个具有以下结构的文件:

[section1]
item1 = text1
item2 = text2
item3 = text3

[section2]
item1 = text4
item2 = text5
item3 = text6

...
我会找到一段文字。e、 g.文本为“text6”,应返回“section2”

我打成平局:

content.scan(/(^\[.*?\]).*?#{text}/m)
但这只是返回文件的第一部分:/ (我使用ruby)

这应该有效:)

/\[([^\[]*?)\][^\[]*?text6/m