I'm asking for input for a dice game. It really matters whether or not the number entered is divisible by ten.
I have \d+0 for the numbers that DO end in zero.
I need one for the number that DO NOT end in zero.
Thanks in advance.
To match any number from 0 to 9 we use \d in regex. It will match any single digit number from 0 to 9. \d means [0-9] or match any number from 0 to 9. Instead of writing 0123456789 the shorthand version is [0-9] where [] is used for character range.
'?' is also a quantifier. Is short for {0,1}. It means "Match zero or one of the group preceding this question mark." It can also be interpreted as the part preceding the question mark is optional. e.g.: pattern = re.compile(r'(\d{2}-)?\
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.
A regular expression followed by an asterisk ( * ) matches zero or more occurrences of the regular expression. If there is any choice, the first matching string in a line is used. A regular expression followed by a plus sign ( + ) matches one or more occurrences of the one-character regular expression.
Maybe this would do the trick
\d*[1-9]
This is not a good use of regular expressions.
I suggest the modulus or integer division operators.
if (number % 10) {
// number doesn't end in zero
}
\d+[1-9]
Should work, I think.
This will match at least one digit followed by a non-zero digit.
However, you very likely need to embed this in some way, either by anchoring it:
^\d+[1-9]$
to verify that the complete string only contains that number (but then you can also convert said string to a number and do a mod 10).
The way you have it currently (and also the expression in your question) it would match a number like 1203
without problems for both expressions, since regexes match substrings unless you anchor them (except in some environments where they are anchored by default like that – I think Java does that).
Also this works for at least two digits only, as does the expression you posted in your question. I assume that to be intentional. If not, then the +
should probably be a *
in both cases.
I think
\d*[1-9]
Works better.
I think (d%10==0) is a better way to test divisibility by 10.
Divisibility trick is valid for integers not for decimals.
What if someone tris to verify this:
123.120
It ends with a non significant zero.
So 123.12/X and 123.120/X gives same result Same for 123.12%X and 123.120%X (This last is not valid operation since value is not an integer, so can not get module of a float/number)
Module can only be getted for integer values (integer/integer).
And also someone can be trying to look for:
AnyTextWithNumbersNotEndingOnZero_0 <--- Not valid
AnyTextWithNumbersNotEndingOnZero <--- Valid
So the best an more clear can be somethng like this:
/[0-9]*0$/
Hope helps.
Ah! and if want letters and numbers but last not be a zero:
/[0-9A-Za-z]*0$/
etc
If you want use regular expression , try this regular expression
^([1-9]+)$
Using Plain JavaScript var num =temp;
if(temp % 10 !==0){
//your code
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With