Hi, I'm looking for a regex which can be used together with split. I need this solution in Perl and unfortunatly also in Python, so I need a regex and can't use some special Perl module. My input string has the following properties:
  1. Comma separated 'words'
  2. Escaped commas ("\\,") should be ignored
  3. Words can have the forms 'value' or 'key=value'
  4. The value can be quoted, which should protect included commas: 'key="value, with, commas"'
  5. The value can contain a list which elements are separated by an other character, e.g. ':'
  6. Single elements of such an list can be quoted by itself, while the whole list isn't quoted: 'key=Elem1:Elem2:"Element, with, comma":Elem4'
I only need help to split the string at the commas. The additional steps to split the key-value pairs and the lists is easy then.

Points 1.-3. alone can be simply done with some negative look-behind, but then it gets complicated. The main problem is how to track if the comma is in quotes or not. My RegEx Kung-Fu isn't bad but just not strong enough here, so I ask your help.

Is there a RegEx possible for this or should try to enforce some easier format for the input?

Thanks in advance, and no, it's not some form of homework.
Martin


In reply to Help with regex for complicated key=value string by mscharrer

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.