Improvment of and looping in a regular expression pattern My implemented regex pattern contains two repeating symbols: \d{2}\. and &lt;p&gt;(.*)&lt;/p&gt;. I want to get rid of this repetition and asked myself if there is a way to loop in Python's regular expression implementation.

Note: I do not ask for help to parse a XML file. There are many great tutorials, howtos and libraries. I am looking for means to implement repetition in regex patterns. 

My code:

import re

pattern = '''
&lt;menu&gt;
&lt;day&gt;\w{2} (\d{2}\.\d{2})\.&lt;/day&gt;
&lt;description&gt;
&lt;p&gt;(.*)&lt;/p&gt;
&lt;p&gt;(.*)&lt;/p&gt;
&lt;p&gt;(.*)&lt;/p&gt;
&lt;/description&gt;
'''

my_example_string = '''
&lt;menu&gt;
&lt;day&gt;Mi 03.04.&lt;/day&gt;
&lt;description&gt;
&lt;p&gt;Knoblauchcremesuppe&lt;/p&gt;
&lt;p&gt;Rindsbraten "Esterhazy" (Gem&amp;uuml;serahmsauce)&lt;/p&gt;
&lt;p&gt;mit H&amp;ouml;rnchen und Salat&lt;/p&gt;
&lt;/description&gt;
&lt;/menu&gt;
'''

re.findall(pattern, my_example_string, re.MULTILINE)
Firstly, just for anyone who might read this: DO NOT take this as an excuse to parse your XML with regular expressions. It generally a really really bad idea! In this case the XML is malformed, so its the best we can do.

The regular expressions looping constructs are * and {4} which you already using. But this is python, so you can construct your regular expression using python:

expression = """
&lt;menu&gt;
&lt;day&gt;\w{2} (\d{2}\.\d{2})\.&lt;/day&gt;
&lt;description&gt;
"""

for x in xrange(3):
    expression += "&lt;p&gt;(.*)&lt;/p&gt;"

expression += """
&lt;/description&gt;
&lt;/menu&gt;
"""