On Apr 29, 9:20 am, Paul McGuire <[EMAIL PROTECTED]> wrote: > On Apr 29, 8:46 am, Julien <[EMAIL PROTECTED]> wrote: > > > I'd like to select terms in a string, so I can then do a search in my > > database. > > > query = ' " some words" with and "without quotes " ' > > p = re.compile(magic_regular_expression) $ <--- the magic happens > > m = p.match(query) > > > I'd like m.groups() to return: > > ('some words', 'with', 'and', 'without quotes') >
Oh! It wasn't until Matimus's post that I saw that you wanted the interior whitespace within the quoted strings collapsed also. Just add another parse action to the chain of functions on dblQuotedString: # when a quoted string is found, remove the quotes, # then strip whitespace from the contents, then # collapse interior whitespace dblQuotedString.setParseAction(removeQuotes, lambda s:s[0].strip(), lambda s:" ".join(s[0].split())) Plugging this into the previous script now gives: ('some words', 'with', 'and', 'without quotes') -- Paul -- http://mail.python.org/mailman/listinfo/python-list