Regular expression, matching final group
<p>I'm making a c parser using python, and my function pattern is producing very strange results.</p>

<p>My pattern is:</p>

<pre><code>([\w\*\[\]]*)\W*([A-Za-z_][\w]+)\W*\(\W*([\w ,\*\[\]]*)\)\W*\{([\w\W]+)return.*;\n\};\n
</code></pre>

<p>When I supply the string:</p>

<pre><code>int main(int argc, char* argv[]) {
    int test[] = {};
    return 0;
};

int test(int a, int b) {
    return a*b;
};
</code></pre>

<p>The call to <code>re.findall</code> returns:</p>

<pre><code>('int', 'main', 'int argc, char* argv[]', '\n  return 0;\n};\n\nint test(int a, int b) {\n  return a*b;\n')
</code></pre>

<p>I want it to return two matches, one for each function.</p>

<p>It appears to me that the parser is ignoring the ending <code>};</code> in each function.</p>

<p>How would I correct this?</p>

<p>It's this part:</p>

<pre><code>return.*
</code></pre>

<p>The <code>.</code> is greedy, and it's simply going to match the entire rest of the document.  You could make it non-greedy.</p>

<pre><code>return.*?
</code></pre>

<p>Or match anything <em>except</em> a newline</p>

<pre><code>return[^\n]*
</code></pre>

python c regex