re grouping question
Fredrik Lundh
fredrik at pythonware.com
Thu May 3 23:44:28 EDT 2001
Michael Esveldt wrote:
> I've got the following code:
>
> s = """some text
> - foo
> - bar
> - fnord
> test..."""
> list_re = re.compile(r'( - .+\n)+')
> print list_re.search(s).groups()
>
> which produces, (' - fnord\n',) when what I was expecting was matching
> all the lines. What I'm really looking for is something more like (' -
> foo\n - bar\n - fnord\n',) or (even better) (' - foo\n', ' - bar\n', ' -
> fnord\n').
>>> list_re = re.compile(r'(?s)( - .+\n)+')
>>> print list_re.search(s).groups()
(' - foo\012 - bar\012 - fnord\012',)
>>> list_re = re.compile(r'( - .+\n)')
>>> print list_re.findall(s)
[' - foo\012', ' - bar\012', ' - fnord\012']
Cheers /F
More information about the Python-list
mailing list