Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular expression to match any number NOT ending in zero

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.

like image 401
Kaninepete Avatar asked May 23 '11 17:05

Kaninepete


People also ask

How do I match a number in regex?

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.

What is '?' In regular expression?

'?' 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}-)?\

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.

Which regex means zero or more occurrences?

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.


7 Answers

Maybe this would do the trick

\d*[1-9]
like image 108
Hyperboreus Avatar answered Oct 05 '22 04:10

Hyperboreus


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
}
like image 23
Jason McCreary Avatar answered Oct 05 '22 04:10

Jason McCreary


\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.

like image 21
Joey Avatar answered Oct 05 '22 04:10

Joey


I think

\d*[1-9]

Works better.

like image 23
sidyll Avatar answered Oct 05 '22 06:10

sidyll


I think (d%10==0) is a better way to test divisibility by 10.

like image 23
kennebec Avatar answered Oct 05 '22 04:10

kennebec


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

like image 29
z666zz666z Avatar answered Oct 05 '22 04:10

z666zz666z


If you want use regular expression , try this regular expression

  ^([1-9]+)$  

Using Plain JavaScript var num =temp;

if(temp % 10 !==0){

//your code

}
like image 45
Ved Avatar answered Oct 05 '22 05:10

Ved