Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Try this: </p> <pre><code>import re w = "TEMPLATES = ( ('index.html', 'home'), ('base.html', 'base'))" # find outer parens outer = re.compile("\((.+)\)") m = outer.search(w) inner_str = m.group(1) # find inner pairs innerre = re.compile("\('([^']+)', '([^']+)'\)") results = innerre.findall(inner_str) for x,y in results: print("%s &lt;-&gt; %s" % (x,y)) </code></pre> <p><strong>Output:</strong></p> <pre><code>index.html &lt;-&gt; home base.html &lt;-&gt; base </code></pre> <p><strong>Explanation:</strong></p> <p><code>outer</code> matches the first-starting group of parentheses using <code>\(</code> and <code>\)</code>; by default <code>search</code> finds the longest match, giving us the outermost <code>( )</code> pair. The match <code>m</code> contains exactly what's between those outer parentheses; its content corresponds to the <code>.+</code> bit of <code>outer</code>.</p> <p><code>innerre</code> matches exactly one of your <code>('a', 'b')</code> pairs, again using <code>\(</code> and <code>\)</code> to match the content parens in your input string, and using two groups inside the <code>' '</code> to match the strings inside of those single quotes.</p> <p>Then, we use <code>findall</code> (rather than <code>search</code> or <code>match</code>) to get all matches for <code>innerre</code> (rather than just one). At this point <code>results</code> is a list of pairs, as demonstrated by the print loop. </p> <p><strong>Update:</strong> To match the whole thing, you could try something like this:</p> <pre><code>rx = re.compile("^TEMPLATES = \(.+\)") rx.match(w) </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload