I'm having trouble to compute 2 regex in one (used to deal with .ini files)
I've got this one (I suggest you to use rubular with theses examples to understand)
^(?<key>[^=;\r\n]+)=((?<value>\"*.*;*.*\"[^;\r\n]*);?(?<comment>.*)[^\r\n]*)
to match :
- This="isnot;acomment"
- This="isa";comment
- This="isa;special";case
And I've got this one :
^(?<key>[^=;\r\n]+)=(?<value>[^;\r\n]*);?(?<comment>[^\r\n]*)
to match
- This=isasimplecase
- This=isasimple;comment
And I'm trying to merge the 2 regex, sadly I do not manage to say "If my value group is not starting with \" use the second one if not use the first one".
Right now i've got this :
^(?<key>[^=;\r\n]+)=(((?<value>\"*.*;*.*\"[^;\r\n]*);?(?<comment>.*)[^\r\n]*)|(?<value>[^;\r\n]*);?(?<comment>[^\r\n]*))
But it's creating 2 more sections unnamed for the simple case without quoted. I was thinking that maybe by adding "the first item of the value group for the simple case must not start with \". But I didn't manage to do it.
PS : I suggest you to use rubular to understand better my problem. Sorry if I wasn't clear enough
How about this?
DEMO
(?<key>[^=;\r\n]+)
Matches the part before the=
symbol."[^"]*"
Matches the string within the double quotes , ex strings like"foobar"
. If there is no"
then the regex engine move on to the next pattern that is[^;\n\r]*
and it matches upto the first;
or newline or\r
character. These matched characters are stored into a named group calledvalue
.;?
Optional semicolon.(?<comment>.*)
Remaining characters are stored into thecomment
group.