Regex for Git Version Tag
Regex Pattern
^v?(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)$Git version tag with optional v prefix (v1.0.0 or 1.0.0)
Quick Answer
The regex pattern for git version tag is `^v?(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)$`. Git version tag with optional v prefix (v1.0.0 or 1.0.0). This works in JavaScript, Python, Ruby, PHP, Java, and most regex engines that support PCRE syntax.
Test Examples
| Input | Result |
|---|---|
| v1.0.0 | ✓ Matches |
| 2.3.4 | ✓ Matches |
| v0.1.0 | ✓ Matches |
| 1.0 | ✗ No match |
| version-1.0.0 | ✗ No match |
| v01.0.0 | ✗ No match |
Code Examples
javascript
const regex = /^v?(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)$/; const isValid = regex.test(value);
python
import re
pattern = r'^v?(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)$'
if re.match(pattern, value):
print("valid")ruby
pattern = /^v?(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)$/ if value =~ pattern puts "valid" end
php
if (preg_match('/^v?(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)$/', $value)) {
echo "valid";
}java
String pattern = "^v?(0|[1-9]\\d*)\\.(0|[1-9]\\d*)\\.(0|[1-9]\\d*)$"; boolean isValid = value.matches(pattern);
Frequently Asked Questions
Related Regex Patterns
JavaScript Variable Name
Valid JavaScript variable/identifier name
Python Variable Name
Valid Python identifier name
CSS Class Name
Valid CSS class name (starts with letter, underscore, or hyphen)
CSS ID Selector
CSS ID selector starting with #
JSON Key
JSON object key (quoted string followed by colon)
Semantic Version (Strict)
Strict semantic versioning (MAJOR.MINOR.PATCH)