deniz
deniz

Reputation: 187

Regex does not match the whole string

Following is the regular expression pattern I'm dealing with:

[\t\v\f ]*([^=\s]+)[\t\v\f ]*=[\t\v\f ]*([^=\s]+)[\t\v\f ]*(?:\r?\n\s*([^=\s]+)[\t\v\f ]*=[\t\v\f ]*([^=\s]+)\s*)*

It basically tries to match key value pairs in a single section of a .ini file. So, for instance, it should be able to match the whole string below:

"aa = 11\nbb = 22\ncc = 33"

I tried to test it on this regex matching website and some others as well and they all seem to match only the first 2 lines. Here is how the match looks like (global flag is disabled):

enter image description here

However when I try to force the regex to find all 3 lines as follows:

[\t\v\f ]*([^=\s]+)[\t\v\f ]*=[\t\v\f ]*([^=\s]+)[\t\v\f ]*(?:\r?\n\s*([^=\s]+)[\t\v\f ]*=[\t\v\f ]*([^=\s]+)\s*){2}

Then it seems to be able to match the whole string.

Can anyone give me a good reason as to why the entire string above does not match my regex? Also what regex should I use in order to match all key value pairs in a string like the one I wrote above?

Upvotes: 0

Views: 472

Answers (1)

Nick
Nick

Reputation: 147206

Your problem is the \s* at the end of the non-capturing group; this is being greedy and absorbing the vertical white-space at the end of the line containing bb = 22 and preventing the group matching again on the line with cc = 33 in it. Changing that to [\t\v\f ] (or even \s*?) makes the regex match the entire string as desired. See demo on regex101. The reason it works when you add the {2} quantifier is that the desire to match makes the engine backtrack when processing the \s* to a point where it can then match the non-capturing group again.

Upvotes: 1

Related Questions