Forem

Cover image for Fun With Regular Expressions
Meir Gabay
Meir Gabay

Posted on • Edited on • Originally published at meirg.co.il

4 2

Fun With Regular Expressions

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]
Enter fullscreen mode Exit fullscreen mode

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]+$
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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

Image of Timescale

🚀 pgai Vectorizer: SQLAlchemy and LiteLLM Make Vector Search Simple

We built pgai Vectorizer to simplify embedding management for AI applications—without needing a separate database or complex infrastructure. Since launch, developers have created over 3,000 vectorizers on Timescale Cloud, with many more self-hosted.

Read more →

Top comments (0)

AWS Security LIVE!

Tune in for AWS Security LIVE!

Join AWS Security LIVE! for expert insights and actionable tips to protect your organization and keep security teams prepared.

Learn More

👋 Kindness is contagious

Dive into an ocean of knowledge with this thought-provoking post, revered deeply within the supportive DEV Community. Developers of all levels are welcome to join and enhance our collective intelligence.

Saying a simple "thank you" can brighten someone's day. Share your gratitude in the comments below!

On DEV, sharing ideas eases our path and fortifies our community connections. Found this helpful? Sending a quick thanks to the author can be profoundly valued.

Okay