我有一个带有两个 OR'd 组的正则表达式,我想知道是否可以让一个组仅在匹配时作为反向引用?在所有情况下,我都想匹配spam.eggs.com
例子:
import re
monitorName = re.compile(r"HQ01 : HTTP Service - [Ss][Rr][Vv]\d+\.\w+\.com:(\w+\.\w+\.(?:net|com|org))|(\w+\.\w+\.(?:net|com|org))")
test = ["HQ01 : HTTP Service - spam.eggs.com",
"HQ01 : HTTP Service - spam.eggs.com - DISABLED",
"HQ01 : HTTP Service - srv04.example.com:spam.eggs.com",
"HQ01 : HTTP Service - srv04.example.com:spam.eggs.com - DISABLED"]
for t in test:
m = monitorName.search(t)
print m.groups()
产生:
(None, 'spam.eggs.com')
(None, 'spam.eggs.com')
('spam.eggs.com', None)
('spam.eggs.com', None)
如果我的小组只返回我的一个匹配组而不是两者都返回,那就太好了。