How can I use Regex to search, have I used the same character in several different places?

So, I am looking at different numbers that have different separators, and I want to find all numbers with the same separator.

Basically, I want the following

123+456+7890 // MATCH
123-456-7890 // MATCH
123.456.7890 // MATCH
123+456-7890 // FAILURE

My current regex that I planned to use was

\d{3}[+-.]\d{3}[+-.]\d{4}

However, it will correspond to numerical sequences that have different delimiters. I don’t want to use one big huge OR for something like that, because in the equivalent of real life there are many other characters that could fit there.

Is there a way to match the same character in multiple places?

+6
source share
2 answers

:

\d{3}([+.-])\d{3}\1\d{4}
  • , [+-.], , .
  • \1 , [+-.], , , .

Regex 101 Demo

+4

, .

^\d{3}([+.-])\d{3}\1\d{4}$
  • ([+.-]) # 1
  • \1

- RegEx

+6

Source: https://habr.com/ru/post/1017221/


All Articles