I'm not sure if my subject is correct but here is what I'm wanting to accomplish. I have a regular expression with two groups that are OR'd and I'm wondering if it's possible to have a group be a back reference only if it matched? In all cases, I'm wanting to match spam.eggs.com
Example:
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()
Produces:
(None, 'spam.eggs.com')
(None, 'spam.eggs.com')
('spam.eggs.com', None)
('spam.eggs.com', None)
It'd be nice if my groups would only return my one matched group and not both. Hope this makes sense