1
爲什麼下一個代碼與單詞不匹配SELECT?匹配組爲什麼存在但不匹配?
import re
re_q = r'(\d{4})-(\d{2})-(\d{2})\s(\d{2}):(\d{2}):(\d{2})\.*\d*\+\d{2}\s|\s(SELECT).*'
raw_q = "2014-01-23 15:28:32.993995+04 | SELECT query_start, query from pg_stat_activity WHERE state='active'"
m = re.match(re_q, raw_q)
for i in range(1, 8):
print "Group <{0}>: {1}".format(i, m.group(i))
輸出:
Group <1>: 2014
Group <2>: 01
Group <3>: 23
Group <4>: 15
Group <5>: 28
Group <6>: 32
Group <7>: None
也許你需要使用['re.findall'(http://docs.python.org/2/library/re.html#re.findall),因爲它匹配在[第二輪]中(http://regex101.com/r/bN9fH1)。 – HamZa