Regexdrill

Level 5: Advanced Exercise 71 of 88 Free

Lookahead: strip leading zeros

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

Each line is a number written with digits only. Remove the zeros at the start of the line, but always leave at least one digit, so 007 becomes 7 and 000 becomes 0.

What this exercise teaches

^0+(?=\d) matches the leading zeros only as far as one digit is left after them. For 000 the engine backs off one zero to satisfy the lookahead, so a single 0 remains.

A hint if you get stuck

^0+ removes too much when the number is all zeros. Add a lookahead that demands a digit after the zeros you remove.

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. The worked solution opens once you solve it or after two tries.

Open this exercise

More exercises in Advanced