Successfully added
Software Development
by Patrik
How to Match Semantic Version Numbers with Regex
When working with software versions like 1.0.2
or 3.45.12-SNAPSHOT
, it's common to use regular expressions (regex) to validate or extract these patterns. This Snipp shows you a simple and effective way to match such version numbers using regex.
What We Want to Match:
1.0.2
3.32.34
3.45.12-SNAPSHOT
These are typical semantic version numbers and may optionally include a suffix like -SNAPSHOT
.
The Regex:
^\d+\.\d+\.\d+(?:-SNAPSHOT)?$
How It Works:
^
— Start of the string\d+
— One or more digits (major, minor, patch)\.
— Dots separating version parts(?:-SNAPSHOT)?
— Optional suffix (non-capturing group)$
— End of the string
Matches:
- ✔️
1.0.2
- ✔️
3.32.34
- ✔️
3.45.12-SNAPSHOT
Not Matched:
- ❌
1.0
- ❌
3.2.4-RELEASE
- ❌
a.b.c
This pattern is useful when validating software version inputs in forms, logs, or build scripts.
regex
versioning
validation
programming
patterns
Referenced in:
Comments