python re.findall()只给我一个结果

时间:2016-05-02 13:06:00

标签: python regex

示例代码:

import re

source = """
BEGIN:VEVENT
DTSTART:20160404T113000Z
DTEND:20160404T141500Z
LOCATION:A3006
SUMMARY:[I-EM-3N:Energy Management Gr 3N] Basics of Mathematical Software
END:VEVENT

BEGIN:VEVENT
DTSTART:20160404T113000Z
DTEND:20160404T141500Z
LOCATION:A3007
SUMMARY:[I-EM-3N:Energy Management Gr 3N] Chemistry and Environment
END:VEVENT
"""

pattern = r'BEGIN.*?DTSTART:(\d{4})(\d{2})(\d{2})T(\d{2})(\d{2})\d{2}Z.*?DTEND:(\d{4})(\d{2})(\d{2})T(\d{2})(\d{2})\d{2}Z.*LOCATION:(.*?)\n.*?SUMMARY:\[(.*?):.*?\]\s(.*?)\s?\n.*?END:VEVENT'


s = re.findall(pattern, source, re.DOTALL)
print s

结果是[(' 2016',' 04',' 04',' 11',' 30&# 39;,' 2016',' 04',' 04',' 14',' 15',&# 39; A3007',' I-EM-3N','化学与环境')],但又在哪一个?

1 个答案:

答案 0 :(得分:3)

你忘了参加一场外卡比赛非贪心(适合我):

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input class="contact-text-filed" maxlength="50" name="subject" id="subject" placeholder="Subject" type="text" value="" class="test" />

而且,由于表达式很复杂且不易阅读,请考虑使用"verbose" mode

BEGIN.*?DTSTART:(\d{4})(\d{2})(\d{2})T(\d{2})(\d{2})\d{2}Z.*?DTEND:(\d{4})(\d{2})(\d{2})T(\d{2})(\d{2})\d{2}Z.*?LOCATION:(.*?)\n.*?SUMMARY:\[(.*?):.*?\]\s(.*?)\s?\n.*?END:VEVENT
                                                                                                           HERE^