Regular expression to check if string contains only zeros

I don´t see why you need a regex, simply convert the string to a number and check if that is 0:

decimal actNumber;
if(decimal.TryParse(myAmount, out actNumber) && actNumber > 0) 
{ /* ... */ }

Thus you can also use the actual number afterwards.


^(?=.*?[1-9])\d+(\.\d+)?$

You can use a simple lookahead for this which will validate if there is at least one [1-9].


If you want a regular expression to check for strings containing only one character, you can just specify that the character be located at the beginning, end, and everywhere in between. Here is an example of how to do so for the digit 0:

regexp '^0+$'

If you are worried about the value containing non-zero digits, you can ensure that no such characters are present using:

regexp '^[^1-9]+$'

Tags:

C#

Regex