I imagine that this question is basic enough that an answer must already exist, but my google-fu skills must be lacking.
I need to parse strings with the following format: upper:lower cc ; ! comment. The character % is used to escape special characters %:; !. The : character delimits upper from lower. The ; character terminates a line. The space character is used to delimit the cc element. Comments are introduced using !. The following strings should be parsed as shown:
a:b c ; upper="a" lower="b" cc="c" comment=""
a%::b c ; upper="a:" lower="b" cc="c" comment=""
a%%:b c ; ! x upper="a%" lower="b" cc="c" comment=" x"
a%!:b c ; ! x upper="a!" lower="b" cc="c" comment=" x"
a%%%::b c ; upper="a%:" lower="b" cc="c" comment=""
What is the most pythonic (i.e. simple, readable, elegant) and robust way to approach this task in python? Are regular expressions suitable?
I tried writing a regular expression that used a negative lookbehind to detect an odd number of %s before the :, but apparently lookbehinds cannot be of variable length.