Share via


regular expression that , Matches all positive decimal floating non-negative numbers less than 8

Question

Friday, May 23, 2008 12:12 PM

i need a regular expression that , Matches all positive decimal floating non-negative numbers less than 8.
this should also allows empty string, as well as check for the presence of alphanumeric characters not entered as a  parameter.

i tried the following regular expressions, but this is not helping me.

 (\|\){0,1}[8-9]+[0-9]*\{0,1}[0-9]*

 

(\|\){0,1}[0,7]\[0-9]+[0-9]* 

 

pls advice me in this regards

cheers

jasu 

 

All replies (11)

Monday, May 26, 2008 6:52 AM âś…Answered

Almost, that will also match "8.0.0". Drop the final "*" (and a few parentheses):

^([0-7](\.\d+)?|8(\.0+))$

(Can drop the parentheses because the alternation operator "|" has the lowest precedence.) 

 

You have to keep the ".0" part of the number optional, so that 8 will be valid too. Instead of the * I used the correct would be ?.

^([0-7](\\d+)?|8(\0+)?)$

(you're also right about the parentheses, but I like them because I think it makes the expression easier to understand specially to those who are not so familiar with regex) 


Friday, May 23, 2008 12:48 PM

You could simlply use RangeValidator:

<asp:RangeValidator ID="RangeValidator1" runat="server" ControlToValidate="TextBox1" ErrorMessage="Invalid value!" MaximumValue="8" MinimumValue="0" Type="Double" />


Friday, May 23, 2008 12:55 PM

 thanks kipo,

i tried using this approach initially , but the RangeValidator , is not firing the needed event , so i prefered using the regular expression.

that what is working with my code, i dont know the reason for the range validatior not working, any suggestion regarding this from your end.

cheers

jasu 


Friday, May 23, 2008 1:01 PM

Can you explain how doesn't RangeValidator works?


Friday, May 23, 2008 1:18 PM

 I think this should do:

^(\|-)?\d+(\\d+)?$

Hope it helps. 


Saturday, May 24, 2008 9:02 AM

hi gbogea,

thanks for Regular expression, but this does not hels me.

This expression ( ^(\|-)?\d+(\\d+)?$ ) , is allowing me to enter number larger than 8. which is faulty.

can you help me with a better expression

cheers

jasu

 


Saturday, May 24, 2008 9:18 AM

I'm still not sure why you can't use RangeValidator, but you can try with this:
^[0-7]{1}(\\d+)?$


Saturday, May 24, 2008 9:21 AM

 Sorry about that. So you need only positive numbers with value lower than 8. Do you mean: 8 > x >= 0 ? or any repetition of numbers lower than 8? (43276342343.327483)

^(\)?[0-7](\\d+)?$ (first case)

^(\)?[0-7]+(\\d+)?$ (second case)


Saturday, May 24, 2008 10:02 AM

Hi everybody,

thanks to gbogea & kipo

this is what my actual requirement is , 8 => x >= 0

the matches for the regular expression is given below 

matches : 8.0, 0 , 7.99, 2, 3

non matches : 8.1, 99, 100

 

cheers

jasu

 

 


Saturday, May 24, 2008 10:44 AM

 Ok, try this:

^(([0-7](\\d+)?)|(8(\0+)*))$

I think it should solve your problem 


Monday, May 26, 2008 5:24 AM

^(([0-7](\\d+)?)|(8(\0+)*))$

 

Almost, that will also match "8.0.0". Drop the final "*" (and a few parentheses):

^([0-7](\.\d+)?|8(\.0+))$

(Can drop the parentheses because the alternation operator "|" has the lowest precedence.)