Regular expression to match any number not ending with zero

I ask you to introduce a dice game. It really matters if the entered number is divided by ten.

I have \ d + 0 for numbers that end in zero.

I need one for a number that DOES NOT end in zero.

Thanks in advance.

+6
source share
8 answers
\d+[1-9] 

Should work, I think.

This will correspond to at least one digit followed by a non-zero digit.

However, you most likely need to somehow insert this, or by binding it:

 ^\d+[1-9]$ 

to make sure that the full line contains only this number (but then you can also convert the specified line to a number and make mod 10).

The way you currently have this (as well as the expression in your question) will correspond to a number like 1203 without problems for both expressions, since regular expressions correspond to substrings if you don't bind them (except in some environments where they are bound by default - I think Java does this).

This also works for at least two digits, as does the expression you posted in your question. I guess this is intentional. If not, then + should probably be * in both cases.

+5
source

Maybe this would do the trick

 \d*[1-9] 
+15
source

It is not very convenient to use regular expressions.

I suggest a module or entire division operators.

 if (number % 10) { // number doesn't end in zero } 
+12
source

I think,

 \d*[1-9] 

It works better.

+2
source

I think (d% 10 == 0) is the best way to check for divisibility by 10.

+2
source

The divisibility trick is valid for integers not for decimals.

What if someone trish checks this:

 123.120 

It ends with an insignificant zero.

So, 123.12 / X and 123.120 / X gives the same result. The same for 123.12% X and 123.120% X (This is the last invalid action, since the value is not an integer, therefore it cannot get a module with a float / number)

The module can be obtained only for integer values ​​(integer / integer).

And also someone might try to find:

 AnyTextWithNumbersNotEndingOnZero_0 <--- Not valid AnyTextWithNumbersNotEndingOnZero <--- Valid 

So, better than something more clear like this:

 /[0-9]*0$/ 

Hope helps.

Oh! and if you want letters and numbers, but the latter should not be zero:

 /[0-9A-Za-z]*0$/ 

etc.

+1
source

If you want to use regex try this regex

  ^([1-9]+)$ 

Using plain JavaScript var num = temp;

 if(temp % 10 !==0){ //your code } 
+1
source

You can try this to exclude 0

 \d+[^0]? 
0
source

Source: https://habr.com/ru/post/888841/


All Articles