Home > database >  How to find repeated word in a string with regex
How to find repeated word in a string with regex

Time:11-14

I have a string like codecodecodecodecode...... I need to find a repeated word in that string.

I found a way but the regular expression always returns half of the repeated part I want.

^(.*)\1 $

enter image description here

at the group(1) I want to see just "code"

CodePudding user response:

If it is greedy, it will first match till the end of the line, and will then backtrack until it can repeat 1 or more times till the end of the string, and for an evenly divided part like this of 4 words, you can capture 2 words and match the same 2 words with the backreference \1

If you have 5 words like codecodecodecodecode as in your example there will be a single group, as the only repetition it can do until the end of the string is 5 repetitions.

The quantifier should be non greedy (and repeat 1 times to not match an empty string) to match as least as possible characters that can be repeated to the right till the end of the string.

^(. ?)\1 $

regex demo

  • Related