Percentages from 0 to 100
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 percentage: a whole number from 0 to 100 with no leading zeros, optionally followed by a period and one or more decimal digits, then a percent sign. 100 may not have a decimal part.
What this exercise teaches
(?:100|[1-9]?\d(?:\.\d+)?)% allows decimals only on the 0 to 99 branch. \d{1,3} would accept 101 and 007, and putting the decimals outside the group would accept 100.5.
A hint if you get stuck
Treat 100 as its own alternative. The other numbers are one or two digits where the first digit, if there are two, is 1 to 9.
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