RegEx match numbers which have n repeating digits at the end


RegEx match numbers which have n repeating digits at the end



I want to match numbers which have exact n repeating digits at the end in Javascript. However, my regex matches n or more than n digits at the end, and I can't seem to fix that.
i.e n=3, match these:
12333
222
1233334333



12333
222
1233334333



Not match these:
11
12344
122233
123333



11
12344
122233
123333



My regexs(don't work):
(d)1{2}$
[^1](d)1{2}$
(d){3}(?!1)$


(d)1{2}$
[^1](d)1{2}$
(d){3}(?!1)$





Another variant: ^(?:(d)(?!1{3}$))*?(d)2{2}$
– bobble bubble
Jun 30 at 14:51


^(?:(d)(?!1{3}$))*?(d)2{2}$




3 Answers
3



Try this - match the digit right before the repeating digits start, use negative lookahead for said digit, then match 3 repeating digits:




const strs = [
'12333',
'222',
'1233334333',
'11',
'12344',
'123333'];
const re = /(^|(d)(?!2))(d)3{2}$/;
strs.forEach(str => {
if (re.test(str)) console.log('pass ' + str);
});





Way cooler than mine.
– kabanus
Jun 30 at 10:04





Yep, its awesome. I literaly never used the n thing in this part of regex, only for replace. I think this is like the en.wikipedia.org/wiki/Candle_problem : If you have been told you can use them this way, it looks so straightforward, but if not you probably never figure it.
– n3ko
Jun 30 at 13:59



Make sure there is no digit after or before:


/[^1]1{3}$|[^2]2{3}$|[^3]3{3}$|.../gm



where the ... mean repeat for each digit. You can generate this expression with a loop quite easily, and use can use a negative look behind for the first non series digit if preferred.


...



fiddle.





REPEATING digits, and only match digits at the end of the number
– Manually Overridden
Jun 30 at 9:54





@ManuallyOverridden Sorry, I missed that - fixed now.
– kabanus
Jun 30 at 10:02





I think this gives false negative for 111 222 etc.
– n3ko
Jun 30 at 10:16





@n3ko For first line, yes. You can add an optional ? to fix that.
– kabanus
Jun 30 at 10:19



?





Whatever. There IS a regexp which answers the question, and without the 1,2,3... nonsens.
– n3ko
Jun 30 at 13:50


1,2,3...



While this expression is almost resolve you need (it gives false positive for 4 or more repetitions) and maybe this can be fixed too ... i suggest to solve this with no or not only with regex. Maybe a [0-9]{n} and an exact repetition check with a backward loop on the string.




let re= /1{3}$|2{3}$|3{3}$|4{3}$|5{3}$|6{3}$|7{3}$|8{3}$|9{3}$|0{3}$/;

var div = document.getElementById('out');

div.innerHTML += "12333".match(re)+" -- <br/>"
div.innerHTML += "222".match(re) +" -- <br/>"
div.innerHTML += "1233334333".match(re) +" -- <br/>"

// Not match these:
div.innerHTML += "11".match(re) +" -- <br/>"
div.innerHTML += "12344".match(re) +" -- <br/>"
div.innerHTML += "122233".match(re)+" -- <br/>"
div.innerHTML += "123333".match(re)+" -- <br/>"


<div id=out></div>






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

PySpark - SparkContext: Error initializing SparkContext File does not exist

List of Kim Possible characters

Python Tkinter Error, “Too Early to Create Image”