python正则表达式的 greedy
.*? 非贪婪模式,正则最小匹配,默认正则表达式会尽可能最大的匹配结果。
正则表达式greedy
注意 如果在正则表达式中 如有使用() 应该使用search 方法匹配。
这样才能正确的显示匹配结果,否则只会显示()中的匹配内容,如只需要()内的内容除外
正则表达式中的有名分组(?P<NAME> 正则表达式)
groupdict() 返回 分组的字典,key是分组的名字,如果正则serarch到内容,groupdict才会有结果
python正则表达式 中 的有名分组 ,匹配 syslog
LOG_REG = re.compile(r'(?P<logtime>\w+\s+\d+\s+[\d:]+)\s+(?P<hostname>[\w\d._]+)\s+(?P<program>\w+(\[\w+\])?:)\s+(?P<msg>.*)')
s1=‘Apr 16 15:14:33 centosX64_1 ntpd_intres[2701]: host name not found: 0.centos.pool.ntp.org’
s = LOG_REG.search(s1)
s.groupdict()
正则表达式 匹配syslog有名分组