python - "Optional" backreferences in regular expression -
i have regular expression 2 groups or'd , i'm wondering if it's possible have group reference only if matched? in cases, i'm wanting 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"]   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 nice if groups return 1 matched group , not both.
the | operator has precedence applies everything before (from beginning of regex in case) or after it. in regex, if there no "srv04.example.com", isn't checking if string contains "http service"!
your 2 capturing groups identical, there's no point in having both. want have srv*: part optional, right?
try one:
r"hq01 : http service - (?:[ss][rr][vv]\d+\.\w+\.com:)?(\w+\.\w+\.(?:net|com|org))"      
Comments
Post a Comment