0

I'm trying to extract the repeated pattern from a string.

For example with something like "112112112112" I would want to end up with "112".

I've been having problems where I either end up with "1" or "112112".

The patterns can be of any size.

Here's an example of the kind of expressions I've been playing around with.

^(.+)(?=\1)
James B
  • 474
  • 2
  • 10
  • What are your exact requirements? You may get the result you want using [`(\d{3})(?=\1)`](https://regex101.com/r/tXcThA/1). – Wiktor Stribiżew Sep 26 '19 at 20:58
  • The length of the pattern can change. – James B Sep 26 '19 at 21:00
  • don't quote me on this, but wouldn't this be an example of an irregular language? – Cease Sep 26 '19 at 21:01
  • Yes, so, what are the requirements? How does the length vary? – Wiktor Stribiżew Sep 26 '19 at 21:01
  • It could be anything from one digit repeating over and over to fairly large pattern that repeats. I'm looking at infinite number sequences that repeat and I'm trying to say how long their pastern are. – James B Sep 26 '19 at 21:05
  • Well, probably `(?=(.+)(?=\1))` will do the job - grab Group 1 values. – Wiktor Stribiżew Sep 26 '19 at 21:05
  • 1
    112112112112, but 112112 is also another pattern within that string. I don't think your question is a duplicate to the other question as the other question is about overlap matching and not about pattern analysis. – Kevin Ng Sep 27 '19 at 01:21

1 Answers1

-1

There are repeated patterns with different sizes, if 3 would be desired, for instance, we'd use a quantifier for that, such as:

(.{3})(?=\1)

Demo 1

or

(.{3,5})(?=\1)

Demo 2

Emma
  • 27,428
  • 11
  • 44
  • 69