0

I have a requirement to capture multiple phrases available in a line (or a input string).
Let's say input is = "this is description,this is description"
So I need to detect the string "this is description" has repeated twice in the input.

This is the regex I've tried so far

(.*).*\1

But it matches input that doesn't have exact repetitions like "this is description,more text,this is description"

Alex Taylor
  • 8,343
  • 4
  • 25
  • 40
greatb
  • 5
  • 1
  • Please post a [mcve]. – shmosel Feb 28 '17 at 21:37
  • Why isn't it working? It [is](https://regex101.com/r/6gCwbt/1). – Wiktor Stribiżew Feb 28 '17 at 21:42
  • The regex will tell you in the pattern appears in the string. In your example, you have artificially declared 2 phrases in one string. How is your code supposed to know that? You should first separate the different phrases into their own strings, and then test each string by itself. – AntonH Feb 28 '17 at 21:43

1 Answers1

0

To find repeating pairs of comma-separated strings:

(.+?),\1

To find any number of repeating values (comma-separated):

(.+?)(?:,\1)+

These work by doing a non-greedy match up to the first comma and then a repeat after the comma.

Alex Taylor
  • 8,343
  • 4
  • 25
  • 40
  • Thanks, but these are capturing this as well which my requirement dont want like "this is test,this is not test" – greatb Mar 24 '17 at 14:44