Re: Issue with regular expressions
- From: Gerard Flanagan <grflanagan@xxxxxxxxx>
- Date: Wed, 30 Apr 2008 08:16:38 -0700 (PDT)
On Apr 29, 3:46 pm, Julien <jpha...@xxxxxxxxx> 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.
.
- References:
- Issue with regular expressions
- From: Julien
- Issue with regular expressions
- Prev by Date: Re: Code/test ratio wrt static vs dynamic typing [was: Re: Python Success stories]
- Next by Date: printing inside and outside of main() module
- Previous by thread: Re: Issue with regular expressions
- Next by thread: Re: Windows Printing using win32print
- Index(es):
Relevant Pages
|
|