Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular expression challenge to match same numbers separately

Tags:

regex

I am struggling with a nice challenge to match two same numbers separately, with a regex.

See here the list I am trying to match separately.

1,680,000,0001,680,000,000
3,350,0003,350,000
110,000110,000
11,100,00011,100,000
550,000550,000
1,0001,000
250250
49,50049,500
165,000165,000
49,50049,500
3,350,0003,350,000
165,000165,000
550,000550,000
550,000550,000
33,10033,100
18,10018,100
450,000450,000

Take for example 550,000550,000, that's twice 550,000 or 250250 that's twice 250. I want to match for example 550,000 and 250.

I have tested many regular expressions in RegexBuddy, but no one does what I want. Maybe you have a suggestion?

like image 392
DelphiLynx Avatar asked Jan 29 '26 13:01

DelphiLynx


1 Answers

If I understand your requirements correctly, then

^(.+)\1$

should work. You can restrict the possible matches to only allow digits and commas like this:

^([\d,]+)\1$

This matches a "double number" and keeps the first repetition in capturing group number 1. If you want your match only to contain the first repetition, then use

^([\d,]+)(?=\1$)

RegexBuddy screenshot

like image 193
Tim Pietzcker Avatar answered Feb 01 '26 15:02

Tim Pietzcker



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!