Python (re)

Semantic Version (SemVer) in PY

Match semantic version strings like 1.2.3, 1.2.3-beta.1, or 1.2.3+build.42.

Try it in the PY tester →

Pattern

regexPY
(?:0|[1-9]\d*)\.(?:0|[1-9]\d*)\.(?:0|[1-9]\d*)(?:-[\w.]+)?(?:\+[\w.]+)?   (flags: g)

Python (re) code

pyPython
import re

pattern = re.compile(r"(?:0|[1-9]\d*)\.(?:0|[1-9]\d*)\.(?:0|[1-9]\d*)(?:-[\w.]+)?(?:\+[\w.]+)?")
input_text = "1.0.0"
for m in pattern.finditer(input_text):
    print(m.group(0))

Stdlib `re` module — no third-party dependency. Works on Python 3.6+.

How the pattern works

Three numeric components (no leading zeros) separated by dots. Optional pre-release label after - and optional build metadata after + per the SemVer 2.0.0 spec.

Examples

Input

1.0.0

Matches

  • 1.0.0

Input

2.3.1-beta.1

Matches

  • 2.3.1-beta.1

Input

1.0.0+build.42

Matches

  • 1.0.0+build.42

Same pattern, other engines

← Back to Semantic Version (SemVer) overview (all engines)