Reputation: 23
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.
Upvotes: 2
Views: 496
Reputation: 4095
How about this:
Test string:
tagaga
Regex:
(?=(aga)).{2}(?<=.)(\1)
Matches:
"aga", "aga"
Working regex example:
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"
Upvotes: 1
Reputation: 369274
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
Upvotes: 0