Re: Issue with regular expressions



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.
.



Relevant Pages

  • Re: inbuilt function buffer()
    ... Say you have a string x. Taking ... using buffer - buffer- will not. ... The web server reads a local file into memory, ... def takebuf: ...
    (comp.lang.python)
  • Re: read, write, seek method in a ring buffer class
    ... Actually i need to create a ring buffer class, with read, write, seek ... similarly write method should take string as a argument. ... # initialize the maximum ... def read ...
    (comp.lang.ruby)
  • Re: read, write, seek method in a ring buffer class
    ... i need to use buffer as a string. ... # initialize the maximum ... def read ...
    (comp.lang.ruby)
  • Re: Discovering variable types...
    ... >- but I suppose MS expect us to use wrappers ... memory allocations for your variables from disk as well. ... >They most certainly are of fixed size, changing the size of a String ... >>me to keep buffer size and current postion right in the memory block. ...
    (comp.lang.pascal.delphi.misc)
  • Re: Secure C library
    ... I read much of the new "security TR", and gee, I don't know. ... the buffer from the buffer size. ... It is not hard to design a better form of buffer and string handling. ... but this is just one example of how thoughtful interface design can ...
    (comp.std.c)