Ask Your Question
3

What is the regular expression to match numbers from 0 to 999, including those with a leading 0?

asked 2022-03-12 11:00:00 +0000

huitzilopochtli gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2021-09-30 06:00:00 +0000

djk gravatar image

The regular expression to match numbers from 0 to 999, including those with a leading 0 is:

\b(0*\d{1,2}|[1-9]\d{0,2})\b

Explanation:

  • \b is used to indicate word boundaries to ensure that the expression only matches numbers and not parts of larger numbers.
  • 0*\d{1,2} matches numbers with a leading 0 (if any) and 1 or 2 additional digits.
  • [1-9]\d{0,2} matches numbers that don't have a leading 0 and have 1, 2, or 3 digits.

Together, the two options cover all possible numbers from 0 to 999.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2022-03-12 11:00:00 +0000

Seen: 9 times

Last updated: Sep 30 '21