Python regex search or match not working
<p>I wrote this regex:</p>

<pre><code> re.search(r'^SECTION.*?:', text, re.I | re.M)
 re.match(r'^SECTION.*?:', text, re.I | re.M)
</code></pre>

<p>to run on this string:</p>

<pre><code>text = 'SECTION 5.01. Financial Statements and Other Information. The Parent\nwill furnish to the Administrative Agent:\n          (a) within 95 days after the end of each fiscal year of the Parent,\n     its audited consolidated balance sheet and related statements of income,\n     cash flows and stockholders\' equity as of the end of and for such year,\n     setting forth in each case in comparative form the figures for the previous\n     fiscal year, all reported on by Arthur Andersen LLP or other independent\n     public accountants of recognized national standing (without a "going\n     concern" or like qualification or exception and without any qualification\n     or exception as to the scope of such audit) to the effect that such\n     consolidated financial statements present fairly in all material respects\n     the financial condition and results of operations of the Parent and its\n     consolidated Subsidiaries on a consolidated basis in accordance with GAAP\n     consistently applied;\n          (b) within 50 days after the end of each of the first three fiscal\n     quarters of each fiscal year of the Parent, its consolidated balance sheet\n     and related statements of income, cash flows and stockholders\' equity as of\n     the end of and for such fiscal quarter and the then elapsed portion of the\n     fiscal year, setting forth in each case in comparative form the figures for\n     the corresponding period or periods of (or, in the case of the balance\n     sheet, as of the end of) the previous fiscal year, all certified by one of\n     its Financial Officers as presenting fairly in all material respects the\n     financial condition and results of operations of the Parent and its\n     consolidated Subsidiaries on a consolidated basis in accordance with GAAP\n     consistently applied, subject to normal year-end audit adjustments and the\n     absence of footnotes;\n          '
</code></pre>

<p>and i was expecting the following output:</p>

<pre><code>SECTION 5.01. Financial Statements and Other Information. The Parent\nwill furnish to the Administrative Agent:
</code></pre>

<p>but i am getting <code>None</code> as the output.</p>

<p>Please anyone tell me what i am doing wrong here?</p>

<p>The <code>.*</code> will match all the text and since your text doesn't ended with <code>:</code> it returns <code>None</code>. You can use a negated character class instead to get the expected result:</p>

<pre><code>In [32]: m = re.search(r'^SECTION[^:]*?:', text, re.I | re.M)

In [33]: m.group(0)
Out[33]: 'SECTION 5.01. Financial Statements and Other Information. The Parent\nwill furnish to the Administrative Agent:'

In [34]: 
</code></pre>

python regex