3

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')
Jamie
  • 7,075
  • 12
  • 56
  • 86

1 Answers1

2

What you are asking for is a non-capturing group. Instead of (...) you write (?:...), so the group will still be used for matching, but won't be added to the results.

See python's official regex documentation for more information.

mech
  • 2,775
  • 5
  • 30
  • 38