-2

Given a branch name like release-1.0 or release-0.4alpha or release-12.02, I want to parse out the version number using a python 3 regex expression.

What I have is:

#!/usr/bin/python3

import re; 
import sys

arg = sys.argv[1]
regex = r'(?<=(^release-))\d+.\d+(alpha)?$'

match = re.match(regex, arg)

if match:
    print(match.group())
else:
    print('Branch "{}" is not valid release branch.'.format(arg))
    sys.exit(1)

But this fails to match any of the attempted names:

$ ./scripts/bin/get-version-number-from-branch release-1.0
Branch "release-1.0" is not valid release branch.
$ ./scripts/bin/get-version-number-from-branch release-1.0alpha
Branch "release-1.0alpha" is not valid release branch.

I originally built and tested this on https://pythex.org/ and https://regex101.com/.

Any ideas what I am missing?

petezurich
  • 9,280
  • 9
  • 43
  • 57
mistakenot
  • 554
  • 3
  • 14

1 Answers1

1

https://docs.python.org/3/howto/regex.html#match-versus-search

Use search instead of match as:

#!/usr/bin/python3

import re; 
import sys

arg = sys.argv[1]
regex = r'(?<=(^release-))\d+.\d+(alpha)?$'

match = re.search(regex, arg)

if match:
    print(match.group())
else:
    print('Branch "{}" is not valid release branch.'.format(arg))
    sys.exit(1)
mistakenot
  • 554
  • 3
  • 14