ipsum
ipsum

Reputation: 1052

Regex match element left before match

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 tied:

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

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

Upvotes: 0

Views: 160

Answers (1)

samy
samy

Reputation: 324

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

This should work :)

Upvotes: 1

Related Questions