+ 1
new regex question
since my old question seemed to confuse a few of you(I'm terrible at wording things lol), here's a new one I need a regular expression that will match any amount of zeros at the end of a number yes, this is for the challenge An example: Number: 6768097 Trailing Zeros: 0 Number: 267700 Trailing Zeros: 2 number: 577880 Trailing Zeros: 1 You can send your full code, as I already have a code that produces the number that will be tested Actually, I'll just include my code. https://code.sololearn.com/Wm0CPovm9N4L/?ref=app I saved it after experimenting a bit, so if you remove the zeros from the output it should work.
4 Réponses
+ 2
To match everything before the zeroes, you can express it like
([0-9]+[1-9])
Which means, a number, but at the end a digit that's not 0. Then you match all the zeroes after. We also need to consider the case when the number is all zeroes. The full regex then is:
([0-9]+[1-9])?(0*)
And in group 2 you have the trailing zeroes.
EDIT: Though, looking at Anna's answer on the last post, /0+$/ or /0*$/ is perfect if you know that the number is at the end of the line. Nothing wrong with that!
+ 1
Can I have an example of that in my code Schindlabua?
+ 1
Also, the number of zeros at the end is NOT known
The program takes all of the digits in the array and multiplies them. I want it to count the trailing zeros and display them. But i'm terrible at regular expressions xD
+ 1
Jay Matthews Well what's a good way to achieve this?