Negative lookahead: numbers that are not percentages
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
Match every whole number that is not followed by a percent sign. Do not match the leading digits of a percentage either. Numbers are whole, with no decimal points. The whole match is compared.
What this exercise teaches
\d+(?![\d%]) refuses to stop in the middle of a number, so the engine cannot shorten 50 to 5 to escape the percent sign. \d+(?!%) alone matches the 5 in 50%.
A hint if you get stuck
A lookahead after \d+ can be satisfied by giving back a digit, so 50% could match as 5. Forbid a digit as well as a percent sign in the lookahead.
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 exerciseMore exercises in Advanced
- Lookahead: amounts before USDFree
- Negative lookahead: not starting with unFree
- Lookbehind: amounts after $Free
- Negative lookbehind: positive numbers onlyFree
- Lazy vs greedy: the first quoted textFree
- Named groups: year, month, dayFree
- Password rules with lookaheadsFree
- Word boundaries: replace a whole wordFree
- Unicode letters with the u flagFree
- Multiline flag: TODO linesFree
- Backreference across lines: duplicate linesFree
- Multiline: trailing whitespace on every lineFree
- Named groups in a replacementFree
- Multiline anchors: lines ending in a semicolonFree