[issue7132] Regexp: capturing groups in repetitions

David Chambers report at bugs.python.org
Thu Apr 1 12:55:14 CEST 2010

David Chambers <david.chambers.05 at gmail.com> added the comment:

I would find this functionality very useful. While I agree that it's often simpler to extract the relevant information in several steps, there are situations in which I'd prefer to do it all in one go.

The application I'm writing at the moment needs to extract metadata from text files. This metadata actually appears as text at the top of each file. For example:

title: Example title
tags: Django, Python, regular expressions

Example title

Here is the first paragraph.

I had expected something like this to get the job done:

meta = re.match(r'(?ms)(?:^(\S+):\s*(.*?)$\n)+^\s*$', contents_of_file)

Ideally in this case, meta.groups() would return:

('title', 'Example title', 'tags', 'Django, Python, regular expressions')

nosy: +davidchambers

Python tracker <report at bugs.python.org>

More information about the Python-bugs-list mailing list