Regexdrill

Level 3: Real-world validation Exercise 37 of 70 Free

Semantic versions

A regex exercise from Regexdrill. Solve it with a pattern box with live highlighting in your browser, checked against hidden test strings, and it is checked the moment you submit.

The task

The whole line must match. Match a semantic version: three numbers separated by periods, each either 0 or a number with no leading zero, optionally followed by a hyphen and a pre-release label made of letters, digits, periods or hyphens, like 1.0.0-beta.2.

What this exercise teaches

(?:0|[1-9]\d*)\.(?:0|[1-9]\d*)\.(?:0|[1-9]\d*)(?:-[0-9A-Za-z.-]+)? refuses leading zeros like 1.02.0, which \d+ would accept, and keeps the hyphen attached to the label so 1.0.0- fails.

A hint if you get stuck

Write one number as 0|[1-9]\d* inside a non-capturing group and use it three times. Put the pre-release part in an optional group that starts with the hyphen.

Try it now, free, no account needed

Every exercise on Regexdrill is free to try. Open it on the practice page, solve it, and get instant feedback with a hint. A free account adds the worked solution; Pro adds timed mode and the certificate.

Open this exercise

More exercises in Real-world validation