Regexdrill

Level 5: Advanced Exercise 70 of 70 Free

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 exercise

More exercises in Advanced