Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular expression to allow for number values between 1-3 digits?

Tags:

regex

RewriteRule ^gallery/[0-9][0-9][0-9]/$ index.php?gallery_id=$1 

It allows for any number that is three digits in length. I do not know how to allow for less than three digits as well (or more than three for that matter).

like image 318
Mike Moore Avatar asked Aug 22 '10 14:08

Mike Moore


People also ask

How do you specify a range of numbers in a regular expression?

\d for single or multiple digit numbers To match any number from 0 to 9 we use \d in regex. It will match any single digit number from 0 to 9. \d means [0-9] or match any number from 0 to 9. Instead of writing 0123456789 the shorthand version is [0-9] where [] is used for character range.

What's the difference between () and [] in regular expression?

[] denotes a character class. () denotes a capturing group. (a-z0-9) -- Explicit capture of a-z0-9 . No ranges.

What does \\ mean in regular expression?

\\. matches the literal character . . the first backslash is interpreted as an escape character by the Emacs string reader, which combined with the second backslash, inserts a literal backslash character into the string being read. the regular expression engine receives the string \. html?\ ' .

What does * do in regular expression?

The Match-zero-or-more Operator ( * ) This operator repeats the smallest possible preceding regular expression as many times as necessary (including zero) to match the pattern.


1 Answers

You want to do something along the lines of

[0-9]{1,3} 

There are some excellent examples here. Scroll down to nearly the bottom of the page, there are examples of how the various range selections (not the right term for them) work.

like image 138
Roman Avatar answered Oct 09 '22 13:10

Roman