Regex: Detect the presence of overlapping or non-overlapping repeating patterns of a given length

StackOverflow https://stackoverflow.com/questions/21643002

  •  08-10-2022
  •  | 
  •  

Pergunta

Is this possible to do with regex?

For example, in: "tagaga", I'd like to match "aga" because it occurs more than once.

'(.{3})(.*)\1'

finds non-overlapping matches (matches "agacaga" in "tagacaga") but not overlapping matches.

However, using look-ahead in this way does not work for me:

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

Alternatively, if the regex solution doesn't exist, is there a dynamic programming solution for this?

Ultimately, I only care about presence and do not need the matched string. I am working in MATLAB, if it makes any difference.

Foi útil?

Solução

How about this:

Test string:

tagaga

Regex:

(?=(aga)).{2}(?<=.)(\1)

Matches:

"aga", "aga"

Working regex example:

http://regex101.com/r/uT5fS1

However depending on the length if the match, ie. in your example aga length is 3, so you would have to modify the quantifier to the length -1. (in this case {2}). So.. If your match was abca you would have to change the quantifier to {3}.

So with test example:

abcabca

Regex:

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

Matches:

"abca", "abca"

Outras dicas

Move lookahead part to the middle:

(.+?)(?=(.+?))\1\2

Example in Javascript:

/(.+?)(?=(.+?))\1\2/.test('asdf')     // false
/(.+?)(?=(.+?))\1\2/.test('tagaga')   // true
/(.+?)(?=(.+?))\1\2/.test('tagacaga') // false
/(.+?)(?=(.+?))\1\2/.test('agag')     // false
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top