I have the following formatted sample string:
== header == information about things ==headeragain== info can have characters like.*?{=
etc on just one line.
I want to parse this in to a hash such that the keys are the “==.+?==” and the values are the info after the keys. I’ve tried a couple of regular expressions to globally match these pairs:
%hash = $string =~ /(==.+?==)(.+)/g
and
%hash = $string =~ /(==.+?==)(.+?)/g
Will match the first key and then everything else as its value, and match just the keys respectively.
%hash = $string =~ /(==.+?==)(.+(?===.+?==))/g
is supposed to look ahead for the next key, but not “eat it up” as I understand it. However, it will only match the first pair and go no further.
I think this problem has come from a misunderstanding of how the global modifier acts. Do I need to tweak something in one of my expressions? Or do I need to be doing something completely different?
Even though you’re using non-greedy modifier, there’s no limitation for 2nd subgroup in you 2nd example.
Add positive look-ahead:
(?=$|==)after value. Here(?=is a declaration of look-ahead block and$or==is a substring, you’re searching for.I.e. the solution is:
/(==.+?==)(.+?)(?=$|==)/g