Issue with regular expressions
Hrvoje Niksic
hniksic at xemacs.org
Tue Apr 29 10:41:35 EDT 2008
Julien <jphalip at gmail.com> writes:
> I'm fairly new in Python and I haven't used the regular expressions
> enough to be able to achieve what I want.
> 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 don't think you can achieve this with a single regular expression.
Your best bet is to use p.findall() to find all plausible matches, and
then rework them a bit. For example:
p = re.compile(r'"[^"]*"|[\S]+')
p.findall(query)
['" some words"', 'with', 'and', '"without quotes "']
At that point, you can easily iterate through the list and remove the
quotes and excess whitespace.
More information about the Python-list
mailing list