Re: regexp for sequence of quoted strings

2005-05-30 Thread Magnus Lycka
gry@ll.mit.edu wrote: > I have a string like: > {'the','dog\'s','bite'} > or maybe: > {'the'} > or sometimes: > {} ... > I want to end up with a python array of strings like: > > ['the', "dog's", 'bite'] Assuming that you trust the input, you could always use eval, but since it seems fairly ea

Re: regexp for sequence of quoted strings

2005-05-27 Thread Paul McGuire
George - Thanks for your enthusiastic endorsement! Here are some quibbles about your pyparsing grammar (but really, not bad for a first timer): 1. The Word class is used to define "words" or collective groups of characters, by specifying what sets of characters are valid as leading and/or body ch

Re: regexp for sequence of quoted strings

2005-05-27 Thread gry
PyParsing rocks! Here's what I ended up with: def unpack_sql_array(s): import pyparsing as pp withquotes = pp.dblQuotedString.setParseAction(pp.removeQuotes) withoutquotes = pp.CharsNotIn('",') parser = pp.StringStart() + \ pp.Word('{').suppress() + \ pp.

Re: regexp for sequence of quoted strings

2005-05-26 Thread Paul McGuire
Ah, this is much better than my crude replace technique. I forgot about str.decode(). Thanks! -- Paul -- http://mail.python.org/mailman/listinfo/python-list

Re: regexp for sequence of quoted strings

2005-05-25 Thread Steven Bethard
Paul McGuire wrote: text = r"'the','dog\'s','bite'" def unquote(s,l,t): > > ... t2 = t[0][1:-1] > ... return t2.replace("\\'","'") > ... Note also, that the codec 'string-escape' can be used to do what's done with str.replace in this example: py> s "'the','dog\\'s','bite'" py> s

Re: regexp for sequence of quoted strings

2005-05-25 Thread Paul McGuire
Pyparsing includes some built-in quoted string support that might simplify this problem. Of course, if you prefer regexp's, I'm by no means offended! Check out my Python console session below. (You may need to expand the unquote method to do more handling of backslash escapes.) -- Paul (Downloa

Re: regexp for sequence of quoted strings

2005-05-25 Thread Alexander Schmolck
gry@ll.mit.edu writes: > I have a string like: > {'the','dog\'s','bite'} > or maybe: > {'the'} > or sometimes: > {} > > [FYI: this is postgresql database "array" field output format] > > which I'm trying to parse with the re module. > A single quoted string would, I think, be: > r"\{'([^']|\\'

Re: regexp for sequence of quoted strings

2005-05-25 Thread Steven Bethard
gry@ll.mit.edu wrote: > I have a string like: > {'the','dog\'s','bite'} > or maybe: > {'the'} > or sometimes: > {} > [snip] > > I want to end up with a python array of strings like: > > ['the', "dog's", 'bite'] > > Any simple clear way of parsing this in python would be > great; I just assume

regexp for sequence of quoted strings

2005-05-25 Thread gry
I have a string like: {'the','dog\'s','bite'} or maybe: {'the'} or sometimes: {} [FYI: this is postgresql database "array" field output format] which I'm trying to parse with the re module. A single quoted string would, I think, be: r"\{'([^']|\\')*'\}" but how do I represent a *sequence* of