匹配颜色对应表:
口诀: 123,123,123,1
123分别用颜色表示。
{"level":"info","message":"Sendno: 1974497337","timestamp":"2017-01-18 08:00:08:545"}
reg = re.compile('\{"level":(?P<remote_ip>[^.]*),"message":(?P<messagea>[^.]*),"timestamp":(?P<time>[^.]*)\}')
####################下面这条是匹配字符更为复杂的######################################
a='{"level":"info","message":"::ffff:10.42.99.232 - - [18/Jan/2017:00:00:05 +0000] \"POST /api/v1/push HTTP/1.1\" 200 84 \"-\" \"-\"\n","timestamp":"2017-01-18 08:00:05:998"}'
reg = re.compile('\{"level":"(?P<remote_ip>[^.]*)","message":"(?P<messagea>.*?)","timestamp":"(?P<time>[^.]*)"}', re.S)
reg = re.compile('\{(?P<remote_ip>[^,]*),(?P<messagea>[^,]*),(?P<time>[^,]*)') #通用无脑匹配型
import re line ='192.168.0.1 25/Oct/2012:14:46:34 "GET /api HTTP/1.1" 200 44 "http://abc.com/search" "Mozilla/5.0"' reg = re.compile('^(?P<remote_ip>[^ ]*) (?P<date>[^ ]*) "(?P<request>[^"]*)" (?P<status>[^ ]*) (?P<size>[^ ]*) "(?P<referrer>[^"]*)" "(?P<user_agent>[^"]*)"') regMatch = reg.match(line) linebits = regMatch.groupdict() print linebits for k, v in linebits.items() : print k+": "+v
python 正则 re 模块常用方法
re.match #从头匹配
re.search #全局匹配第一个符合的字符串