By way of example, say I have these test strings and regex:
git_describes = ['v1.0.0-beta-1-g5d1a5a2', 'v1.0.0-g5d1a5a2', 'v1.0.0-alpha-1-g5d1a5a2', 'v1.0.0-1-g5d1a5a2']
git_regex = r'v([0-9]+.[0-9]+.[0-9]+)(-(beta|alpha))?(-([0-9]+))?-g([a-f0-9]+)'
for g in git_describes:
print re.search(git_regex, g).groups()
Whose output is:
('1.0.0', '-beta', 'beta', '-1', '1', '5d1a5a2')
('1.0.0', None, None, None, None, '5d1a5a2')
('1.0.0', '-alpha', 'alpha', '-1', '1', '5d1a5a2')
('1.0.0', None, None, '-1', '1', '5d1a5a2')
I'm grouping dashes with a conditional (-(beta|alpha))?
, but I'm not interested in seeing them in the final grouped result.
Its a simple matter to remove the 2nd and 4th entry of the tupled results, but how can I write the regex so that they're not included in the first place? i.e.
('1.0.0', 'beta', '1', '5d1a5a2')
('1.0.0', None, None, '5d1a5a2')
('1.0.0', 'alpha', '1', '5d1a5a2')
('1.0.0', None, '1', '5d1a5a2')