Issue with regular expressions
Gerard Flanagan
grflanagan at gmail.com
Wed Apr 30 11:16:38 EDT 2008
On Apr 29, 3:46 pm, Julien <jpha... at gmail.com> wrote:
> Hi,
>
> 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'd like m.groups() to return:
> ('some words', 'with', 'and', 'without quotes')
>
> Is that achievable with a single regular expression, and if so, what
> would it be?
>
> Any help would be much appreciated.
>
With simpleparse:
----------------------------------------------------------
from simpleparse.parser import Parser
from simpleparse.common import strings
from simpleparse.dispatchprocessor import DispatchProcessor, getString
grammar = '''
text := (quoted / unquoted / ws)+
quoted := string
unquoted := -ws+
ws := [ \t\r\n]+
'''
class MyProcessor(DispatchProcessor):
def __init__(self, groups):
self.groups = groups
def quoted(self, val, buffer):
self.groups.append(' '.join(getString(val, buffer)
[1:-1].split()))
def unquoted(self, val, buffer):
self.groups.append(getString(val, buffer))
def ws(self, val, buffer):
pass
groups = []
parser = Parser(grammar, 'text')
proc = MyProcessor(groups)
parser.parse(TESTS[1][1][0], processor=proc)
print groups
----------------------------------------------------------
G.
More information about the Python-list
mailing list