unknown6656
unknown6656

Reputation: 2963

Regex - Match any sequence which repeats itself n times


Do you know how to match a group which repeats itself n times? e.g.:

This is is is is a test       [incl. the spaces between two 'is']
Foo bar
Telelephone: +49 188/123 45 45 45

I tried the following regex pattern: (\w+)\1+
but it only matches two occurences of a group (and not n occurences)

Thank you very much

Upvotes: 5

Views: 1065

Answers (1)

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726599

The problem with your pattern with respect to your sample input is that the pattern does not account for possible trailing spaces:

(\w+\s*)\1+

Demo.

This is not universal, though: for example, this pattern will match "a ba ba ba ba b" as "a ba ba ba ba b", not as "a ba ba ba ba b".

Upvotes: 4

Related Questions