Matching when an arbitrary pattern appears multiple times

后端 未结 1 1114
死守一世寂寞
死守一世寂寞 2021-01-22 20:46

I\'m trying to write a regex that will succeed when an arbitrary pattern of length 2 or greater appears more than once in a phrase. Essentially, I would like to use a capture gr

相关标签:
1条回答
  • 2021-01-22 21:16

    You can use this lookahead based regex:

    (.{2,})(?=.*?\1)
    

    RegEx Demo

    Explanation:

    .{2,}     # any 2 or more characters
    (.{2,})   # group them into captured group #1
    (?=...)   # positive lookahead
    (?=.*?\1) # make sure at least one occurrence of captured group #1
    
    0 讨论(0)
提交回复
热议问题