2016-09-15 7 views
1

を働いていない一致:Pythonの正規表現の検索や、私はこの正規表現を書いた

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   ' 

と、私は次の出力を期待していた:この文字列で実行するように

re.search(r'^SECTION.*?:', text, re.I | re.M) 
re.match(r'^SECTION.*?:', text, re.I | re.M) 

SECTION 5.01. Financial Statements and Other Information. The Parent\nwill furnish to the Administrative Agent: 

私は出力としてNoneを得ています。

ここで間違っていることを教えてください。

+0

dot-allフラグ修飾子 '(?s)^ SECTION。* ?:'をインラインで使うか、コンパイルオプションとして使う必要があります。問題はコロンが別の行にあり、デフォルトのオプションはドット '.'が改行にマッチしないということです。私はすべての修飾子をインラインに入れます: '(?ism)^ SECTION。* ?:' – sln

答えて

1

.*はすべてのテキストと一致し、テキストは:で終わらないため、Noneを返します。

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]: