Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex accept numeric only. First character can't be 0

Tags:

regex

I have a textbox that I created using .NET..

By using that textbox, the user only can key in numeric. But not start with 0. start with 1-9. after the user key in the 1-9 at the first character, the user can key in 0.

Regex reg = null;
reg = new System.Text.RegularExpressions.Regex("^[1-9][0-9]*$")
return reg.IsMatch(str);

That is my regex expression. By using that i can't key in 1-9. but only 0 and alphabet. But if i using ^[1-9] I can key in numeric, but can't key in the 0.

I already tried all the answer that all of you suggest. But still can't work. It's not like i dont read all of your answer.

here is the picture..

enter image description here

I want to validate at the first time, the user only can key in numeric, but start with value that is not 0 or alphabet but 1-9. After the first character, user may only key in 0-9.

i do use Int.TryParse, but i use that after i hit a button to process.

reg = new System.Text.RegularExpressions.Regex("^[^0-9]");

that regex accept only numeric from 0 to 9.

reg = new System.Text.RegularExpressions.Regex("^[^1-9]");

that regex accept only numeric from 1 to 9.

How can i add more expression to regex for the second character until the rest that only accept numeric 0-9?

By the way, i don't care about 0.99 because in here, the price is fix. not with 0.99 or 0.123..

Any others way to do it? thanks.

like image 231
Alfred Angkasa Avatar asked Jan 03 '13 02:01

Alfred Angkasa


People also ask

What does \+ mean in regex?

Example: The regex "aa\n" tries to match two consecutive "a"s at the end of a line, inclusive the newline character itself. Example: "a\+" matches "a+" and not a series of one or "a"s. ^ the caret is the anchor for the start of the string, or the negation symbol.

What is the regex for only numbers?

The \d can be used to match single number. Alternatively the [0-9] can be used to match single number in a regular expression. The [0-9] means between 0 and 9 a single number can match.

How do I find the first character in a regular expression?

The regular expression to match String which contains a digit as first character is “^[0-9]. *$”.

How do you match a character except one regex?

To match any character except a list of excluded characters, put the excluded charaters between [^ and ] . The caret ^ must immediately follow the [ or else it stands for just itself. The character '. ' (period) is a metacharacter (it sometimes has a special meaning).


4 Answers

If you're looking at something like a price, you need to consider that 0.99 is probably perfectly valid. For something like that, I would simply start with a non-complex ^[0-9]*(\.[0-9]{0,2})?$ (again there may be edge cases that may make it more complex like three digits after the decimal point and so on) and allow leading zeroes, since they don't "damage" the value in anyway.

It it must start with a non zero, just change the initial [0-9]* to a [1-9][0-9]*. For integers only (as seems to be indicated by your added sample data), that would be:

^[1-9][0-9]*$
like image 182
paxdiablo Avatar answered Oct 06 '22 20:10

paxdiablo


To match a number starting with any digit but zero:

^[1-9][0-9]*$

And if you want to match 0 as well:

^([1-9][0-9]*)|([0]+)$

remove the last plus if you want a single zero only

To allow any alpha-numeric after first non-zero:

^[1-9a-zA-Z][0-9a-zA-Z]*$
like image 29
perreal Avatar answered Oct 06 '22 21:10

perreal


As your code is .NET you should not use regex to parse an Integer. Just use UInt32.TryParse() method

uint num=0;
if(UInt32.TryParse(str, out num)){
    Console.WriteLine("Converted '{0}' to {1}.", str, num);   
}else{
    Console.WriteLine("conversion of '{0}' failed.", value==null? "": value);
}

Old answer

This simple regular expression will do it ^[1-9]\d*$

like image 22
Shiplu Mokaddim Avatar answered Oct 06 '22 21:10

Shiplu Mokaddim


This is what worked the best for me:

^(?!(0))[0-9]$
like image 31
Jerlam Avatar answered Oct 06 '22 20:10

Jerlam