I’m not so good with regex, and need some help.
I have a string similar to the following:
[{type='(type here)', field='(field here)', value='(value here)'},{...},...,{...}]
I am trying to match it with the following regex:
^\[(\{type=\'(.*)\', field=\'(.*)\', value=\'(.*)\'\},*)*\]$
But it isn’t matching. I then debugged. Here is the regex I used for debugging:
\[(\{(.*)\}\]
Here is the string:
[{type='cost', field='flag & e band 100s ($1/M's)', value='680'},{type='cost', field='29 versions', value='250'}]
Here is the match:
{type=’cost’, field=’flag & e band 100s ($1/M’s)’, value=’680′},{type=’cost’, field=’29 versions’, value=’250′}
I understand why this string was matched. I do not understand why no other strings were matched. I expected the other matched strings to be:
-
{type=’cost’, field=’flag & e band 100s ($1/M’s)’, value=’680′},
-
{type=’cost’, field=’29 versions’, value=’250′}
Why were these matches not made?
The problem is using
.*inside the subgroups. Thetype=\'(.*)\'matches greedily, i.e. it will yieldcost', field='flag & e band 100s ($1/M's)', value='680'}, {type='cost.Also: The delimiters in your data are also present in the content, e.g. your pattern seeks to parse
field=\'(.*)\'but hits hard ontofield='flag & e band 100s ($1/M's)',(note the extra'after the M.So I propose (if you also want to collect the content of the fields):
otherwise, trigger only on the curly brace grouping, i.e.
\{[^\}]+\}