Match multiple times a group in a string
<p>i'am trying to use regular expression. I have this string that has to be matched</p>

<pre><code> influences = 
 {{hlist |[[Plato]] |[[Aristotle]] |[[Socrates]] |[[David Hume]] |[[Adam Smith]] |[[Cicero]] |[[John Locke]]}}
 {{hlist |[[Saint Augustine]] |[[Saint Thomas Aquinas]] |[[Saint Thomas More]] |[[Richard Hooker]] |[[Edward Coke]]}}
 {{hlist |[[Thomas Hobbes]] |[[Rene Descartes]] |[[Montesquieu]] |[[Joshua Reynolds]] |[[Sir William Blackstone|William Blackstone]]}}
 {{hlist |[[Niccolo Machiavelli]] |[[Dante Alighieri]] |[[Samuel Johnson]] |[[Voltaire]] |[[Jean Jacques Rousseau]] |[[Jeremy Bentham]]}}
</code></pre>

<p>I would like to extract from the text the following templates: </p>

<pre><code>{{hlist .... }}
</code></pre>

<p>Instead, the following text has not to be matched:</p>

<pre><code>main_interests = 
 {{hlist |[[Music]] |[[Art]] |[[Theatre]] |[[Literature]]}}
</code></pre>

<p>I wrote this regex but it doesn't work</p>

<pre><code>(?:^\|\s*)?(?:influences)\s*?=\s*?(?:(?:\s*\{\{hlist)\s*\|([\d\w\s\-()*â&amp;;\[\]|#%.&lt;&gt;Â·:/",\'!{}=â¢?â
Ã¡~Ã¼Â°ÅÃ©Ã¶$Ã Ã¨Ã¬Ã²Ã¹ÃÃÃÃÃÃ¡Ã©Ã­Ã³ÃºÃ½ÃÃÃÃÃÃÃ¢ÃªÃ®Ã´Ã»ÃÃÃÃÃÃ£Ã±ÃµÃÃÃÃ¤Ã«Ã¯Ã¶Ã¼Ã¿ÃÃÃÃÃÅ¸Ã§ÃÃÃÃ¸ÃÃ¥ÃÃ¦Å]*?)(?=\n))+
</code></pre>

<p>I'm using python.</p>

<p>You can use a list comprehension with some regular expressions:</p>

<pre><code>import re
string = """
influences = 
 {{hlist |[[Plato]] |[[Aristotle]] |[[Socrates]] |[[David Hume]] |[[Adam Smith]] |[[Cicero]] |[[John Locke]]}}
 {{hlist |[[Saint Augustine]] |[[Saint Thomas Aquinas]] |[[Saint Thomas More]] |[[Richard Hooker]] |[[Edward Coke]]}}
 {{hlist |[[Thomas Hobbes]] |[[Rene Descartes]] |[[Montesquieu]] |[[Joshua Reynolds]] |[[Sir William Blackstone|William Blackstone]]}}
 {{hlist |[[Niccolo Machiavelli]] |[[Dante Alighieri]] |[[Samuel Johnson]] |[[Voltaire]] |[[Jean Jacques Rousseau]] |[[Jeremy Bentham]]}}
"""

matches = [template.group(1) 
           for match in re.findall(r'\{\{hlist.+?\}}', string)
           for template in re.finditer(r'\[\[([^]]+)\]\]', match)]
print(matches)
# ['Plato', 'Aristotle', 'Socrates', 'David Hume', 'Adam Smith', 'Cicero', 'John Locke', 'Saint Augustine', 'Saint Thomas Aquinas', 'Saint Thomas More', 'Richard Hooker', 'Edward Coke', 'Thomas Hobbes', 'Rene Descartes', 'Montesquieu', 'Joshua Reynolds', 'Sir William Blackstone|William Blackstone', 'Niccolo Machiavelli', 'Dante Alighieri', 'Samuel Johnson', 'Voltaire', 'Jean Jacques Rousseau', 'Jeremy Bentham']
</code></pre>

<p>This uses two expressions, one for the outer part (<code>{{hlist...}}</code>) and another one for the inner part (<code>[[...]]</code>).
<hr>
See <a href="http://ideone.com/B7r3Tz" rel="nofollow"><strong>a demo on regex101.com</strong></a>.</p>

python regex