Reputation: 70
I want to write a regular expression that will match a pattern that repeats at least twice, followed by part of the same pattern.
For example, abcabca should match, as should abcabcab, defdefdefde, etcetera.
I think I need to use back references for this. I envision something like ^(.+?){2,}\1$
but somehow matching only part of the \1
back reference.
Given the repeating pattern abc
, I want to match at least 2 occurrences of abc
, followed by part of the string abc
.
These should match:
FooFooF
(This is 2 repeats of Foo
followed by the first letter in Foo
)FooFooFoo
(This is either 3 repeats of Foo, followed by a 0-length substring or 2 followed by the entire word as a substring. It depends on how you look at it.)FooFooFooFo
(This is 3 repeats followed by Fo
, the first two letters in Foo
)These shouldn't:
Foo
(There need to be 2+ repeats)FooFo
(This is only 1 repeat)FooFooFoX
(FoX
is not part of the string Foo
, so it doesn't match.)Is this possible? If so, how can I do it?
Upvotes: 1
Views: 1061
Reputation: 30985
If you want to fit a pattern for 3 character you can use a regex like this:
\b(.{3})\1.*?\b
But if you want to have whatever pattern defined for the first characters then you could use:
\b(.+)\1.*?\b
Upvotes: 2