Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular expression for 1-15? [closed]

Tags:

regex

I'm having difficulty putting together a regular expression for a numeric input of 0-15.

I have tried this expression:

^([9]{1,1}|[0-1][0-5])$   

<asp:FilteredTextBoxExtender ID="TMPFiltered" runat="server" FilterMode="ValidChars"
                                                FilterType="Custom" ValidChars^([9]{1,1}|[0-1][0-5])$" TargetControlID="txtTMP" />

however it is allowing for higher maximums than 15. Where is my syntax incorrect? Any help is appreciated, thanks.

like image 849
tmaurst Avatar asked Feb 19 '26 03:02

tmaurst


2 Answers

Try this:

^([0]?[1-9]|1[0-5])$

or try this if you dont want to match like "07"

^([1-9]|1[0-5])$
like image 143
AnandVeeramani Avatar answered Feb 22 '26 01:02

AnandVeeramani


Regex is not the right solution for this, you should just try to convert the string to an int and if that succeeds make sure the result is in your desired range.

That being said, here is a regex that should work:

^(1[0-5]?|[2-9])$

The primary issue with your current regex is that the anchors are only applied to the left side of the alternation, so you will match strings that match [0-1][0-5] anywhere in the regex. You also don't have any way in your current regex to just match numbers from 2 to 8.

Example: http://www.rubular.com/r/HUNZZymzFW

like image 31
Andrew Clark Avatar answered Feb 22 '26 02:02

Andrew Clark



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!