Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex for range 1-1000 [duplicate]

Tags:

I need help creating a simple regex for a whole number range of 1-1000, with no special characters.

The two I have both seem to break or allow characters or not the full range:

  1. ^\d(\d)?(\d)?$
  2. ^[0-9]{1,3}$
like image 664
user547835 Avatar asked Dec 19 '10 17:12

user547835


People also ask

How do you repeat in regex?

A repeat is an expression that is repeated an arbitrary number of times. An expression followed by '*' can be repeated any number of times, including zero. An expression followed by '+' can be repeated any number of times, but at least once.

What does regex (? S match?

i) makes the regex case insensitive. (? s) for "single line mode" makes the dot match all characters, including line breaks.

What is Backreference regex?

A backreference in a regular expression identifies a previously matched group and looks for exactly the same text again. A simple example of the use of backreferences is when you wish to look for adjacent, repeated words in some text. The first part of the match could use a pattern that extracts a single word.


1 Answers

Try this:

^([1-9][0-9]{0,2}|1000)$
  • [1-9][0-9]{0,2} matches any number between 1999
  • 1000 matches 1000
like image 59
Gumbo Avatar answered Oct 14 '22 15:10

Gumbo