Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular Expression: Numeric range [duplicate]

Tags:

regex

How do you write a regular expression that matches a numeric range from 0 or 000 to 180 ?

like image 665
Morten Avatar asked Sep 04 '09 08:09

Morten


People also ask

How do I match a range of numbers in regex?

The regex [0-9] matches single-digit numbers 0 to 9. [1-9][0-9] matches double-digit numbers 10 to 99. Something like ^[2-9][1-6]$ matches 21 or even 96! Any help would be appreciated.

Is regex faster than for loop?

Regex is faster for large string than an if (perhaps in a for loops) to check if anything matches your requirement.

What is regex for numbers?

Definition and Usage. The [0-9] expression is used to find any character between the brackets. The digits inside the brackets can be any numbers or span of numbers from 0 to 9. Tip: Use the [^0-9] expression to find any character that is NOT a digit.


1 Answers

I don't think regex is the right choice for this. Have you tried parsing the value? If you have to use regex I would match \d{1,3} parse the string and then validate the number in code.

like image 65
stevehipwell Avatar answered Sep 21 '22 22:09

stevehipwell