DelphiLynx
DelphiLynx

Reputation: 921

Regular expression challenge to match same numbers separately

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?

Upvotes: 3

Views: 432

Answers (1)

Tim Pietzcker
Tim Pietzcker

Reputation: 336108

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

Upvotes: 7

Related Questions