regex, repeating group
Riccardo Galli
riccardo_togli.questo at e.questo_sideralis.net
Fri May 30 16:38:38 EDT 2003
On Fri, 30 May 2003 15:32:18 -0400, Steven Scott wrote:
> say I'm working with this:
> str = """body body body
> -><-
> Meeting Info
> From:you
> Attendees:me
> Date:now
> ID:0"""
>
> I have this regular expression to get the data after colons, which I'm using
> in re.search:
>
> regex = r'-><-\n.+?\n(?:.+?:(.*?)\n?){4}$'
>
> my problem is that this (when I call .groups()) only returns the last datum,
> ('0',). if I make the outer group non-grouping I only get ('ID:0',
> '0')....still not any of the other lines of data.
I've never mastered the usage of {}, but if you use
regex = r'(.+:(.+)?\n?)'
and then
re.findall(regex,str)
you'll get
[('From:you\n', 'you'), ('Attendees:me\n', 'me'), ('Date:now\n', 'now'),
('ID:0', '0')]
it's useful.
note: it's really a bad idea use 'str' as variable name, 'cause it's a
type name.
Ciao,
Riccardo
--
-=Riccardo Galli=-
_,e.
s~ ``
~@. ideralis Programs
. ol
`**~ http://www.sideralis.net
More information about the Python-list
mailing list