开发者

Regex match element left before match

开发者 https://www.devze.com 2023-02-10 19:20 出处:网络
I got a file with the following structure: [section1] item1 = text1 item2 = text2 item3 = text3 [section2]

I got a file with the following structure:

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

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

...

and i will find the section to a text. e.g. text is "text6" it should return "section2".

I t开发者_开发百科ied:

content.scan(/(^\[.*?\]).*?#{text}/m)

but that is only returning the first section of the file :/ (i use ruby)


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

This should work :)

0

精彩评论

暂无评论...
验证码 换一张
取 消