Cliff
Cliff

Reputation: 13

Regex match all words from capturing group, not just 2

I have this regex:

\b(\w+)\s+\1{1,}\b

Online test: https://regex101.com/r/iHnSCs/1m

but it only matches 2 consecutive words. How do I match more? I tried a couple of solutions but seems like I'm not getting it right. Thanks.

Upvotes: 1

Views: 41

Answers (1)

CertainPerformance
CertainPerformance

Reputation: 370729

Put the \s+\1 in a non-capturing group, and repeat that group:

\b(\w+)(?:\s+\1)+\b

(either turn off the U flag, or make the repitition greedy with ? - also note that {1,} simplifies to +)

https://regex101.com/r/iHnSCs/2

Upvotes: 1

Related Questions