Fun With Regular Expressions

2 mins
Published on 11 November 2020

Following PEP 440, it’s important to check the semantics of a release version before publishing to PyPi. So I thought to myself, here’s a good way to practice my “regular expression skills”.

The required pattern:

[N!]N(.N)*[{a|b|rc}N][.postN][.devN]

Using a regular expression to check if a release version matches this pattern:

^[0-9]+(\.[0-9]*)*(\.[0-9]+(a|b|rc)|(\.post)|(\.dev))*[0-9]+$

Explaining The Regular Expression

We’ll go over it bit by bit

  • ^[0-9]+ - String must start ^ with at least one or more + digits [0-9]. Matching patterns: 0, 23, 200
  • (...)* - This group () can repeat zero to infinite times *
  • \.[0-9]* - The previous group must start with . (\ backslash escapes .), and zero to infinite number * of digits [0-9]. Matching patterns: ., .1, .23
  • (...)* - This group () can repeat zero to infinite times *
  • \.[0-9]+(a|b|rc) or \.post or \.dev - The previous group must match one of the following patterns: .1a, .1b, .1rc, .post, .dev
  • [0-9]+$ - String must end ($) with at least one digit. Matching patterns: 3, 03, 92

Matching Patterns

PyPa - Packaging and distributing projects

1.2.0.dev1  # Development release
1.2.0a1     # Alpha Release
1.2.0b1     # Beta Release
1.2.0rc1    # Release Candidate
1.2.0       # Final Release
1.2.0.post1 # Post Release
15.10       # Date based release
23          # Serial release

Thoughts

  • Why have I used [0-9] instead of \d? - I found out the hard way that some versions of Bash don’t support \d, so sticking with [0-9] is better
  • Initially, I used (?<=[0-9]) at the end of the string, instead of [0-9]+$. And again, I found out the hard way that positive lookbehind is not supported in some versions of Bash. Also, it’s better to keep it simple, using positive lookahead might look weird to future you

References


Originally published at unfor19/python-project on November 11, 2020

Related Posts