Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Need a regex to match a variable length string of numbers that can't be all zeros

I need to validate an input on a form. I'm expecting the input to be a number between 1 to 19 digits. The input can also start with zeros. However, I want to validate that they are not all zeros. I've got a regex that will ensure that the input is numeric and between 1 and 19 numbers.

^\d[1,19]$

But I can't figure out how to include a check that the entire string is not all zeros. I tried this

^(![0]{1,19})(\d[1,19])$

but it fails on 0000000000000000001 because it's allowing a variable number of zeros.

How do I check that the entire string is NOT zeros?

Thanks.

I'm trying to do this in a ASP.NET RegularExpressionValidator so I was hoping for a single expression. I have other options, so I'm not out of luck if this can't be done.

like image 699
Notorious2tall Avatar asked Feb 06 '09 17:02

Notorious2tall


People also ask

How do I match a range of numbers in regex?

With regex you have a couple of options to match a digit. You can use a number from 0 to 9 to match a single choice. Or you can match a range of digits with a character group e.g. [4-9]. If the character group allows any digit (i.e. [0-9]), it can be replaced with a shorthand (\d).

What does \f mean in regex?

Definition and Usage The \f metacharacter matches form feed characters.

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. 1* means any number of ones.


1 Answers

^(?!0+$)\d{1,19}$

like image 171
chaos Avatar answered Sep 20 '22 23:09

chaos