2016-09-15 1 views
1

내가이 정규식 쓴 작동하지 않는 경기 :파이썬은 검색 정규식 또는

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

이 문자열을 실행 :

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   ' 

나는 다음과 같은 출력 기다리고 있었다 :

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

을 하지만 출력으로 None지고 있어요.

누군가 내가 여기서 잘못하고있는 것을 말해 주시겠습니까?

+0

^제 *?'인라인, 또는 컴파일 옵션 : 당신은 예상 된 결과를 얻기 위해 대신 부정 문자 클래스를 사용할 수 있습니다 (들?).. 문제는 콜론이 다른 줄에 있고 기본 옵션은 점 '.'이 줄 바꿈과 일치하지 않는다는 것입니다. 나는 모든 수정자를 인라인으로 넣을 것이다 :'(? 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]: