Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RegEx for value Range from 1 - 365

Tags:

regex

What is the RegEx for value Range from 1- 365

like image 307
Sreedhar Avatar asked Dec 09 '10 07:12

Sreedhar


People also ask

How do you specify a range in regex?

To show a range of characters, use square backets and separate the starting character from the ending character with a hyphen. For example, [0-9] matches any digit. Several ranges can be put inside square brackets.

What does '$' mean in regex?

$ means "Match the end of the string" (the position after the last character in the string).

What does regex 0 * 1 * 0 * 1 * Mean?

Basically (0+1)* mathes any sequence of ones and zeroes. So, in your example (0+1)*1(0+1)* should match any sequence that has 1. It would not match 000 , but it would match 010 , 1 , 111 etc. (0+1) means 0 OR 1.

What is difference [] and () in regex?

This answer is not useful. Show activity on this post. [] denotes a character class. () denotes a capturing group. [a-z0-9] -- One character that is in the range of a-z OR 0-9.


2 Answers

Try this:

^(?:[1-9]\d?|[12]\d{2}|3[0-5]\d|36[0-5])$
  • The start anchor ^ and end anchor $ are to match the whole input and not just part of it.
  • (? ) is for grouping.
  • | is for alternation
  • [1-9]\d?     matches 1 to 99
  • [12]\d{2}   matches 100 to 299
  • 3[0-5]\d     matches 300 to 359
  • 36[0-5]          matches 360 to 365
like image 77
codaddict Avatar answered Sep 30 '22 13:09

codaddict


You would have to list the possible combinations 1-9, 10-99, 100-299, 300-359, 360-365:

^([1-9]\d?|[12]\d\d|3[0-5]\d|36[0-5])$
like image 32
Guffa Avatar answered Sep 30 '22 14:09

Guffa