Issue with regular expressions
Paul McGuire
ptmcg at austin.rr.com
Tue Apr 29 12:56:26 EDT 2008
On Apr 29, 9:20 am, Paul McGuire <pt... at austin.rr.com> wrote:
> On Apr 29, 8:46 am, Julien <jpha... at gmail.com> 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
More information about the Python-list
mailing list