Match at every second occurrence

前端 未结 6 1609
我在风中等你
我在风中等你 2020-11-28 07:40

Is there a way to specify a regular expression to match every 2nd occurrence of a pattern in a string?

Examples

  • searching for a agains
6条回答
  •  野趣味
    野趣味 (楼主)
    2020-11-28 08:05

    Back references can find interesting solutions here. This regex:

    ([a-z]+).*(\1)
    

    will find the longest repeated sequence.

    This one will find a sequence of 3 letters that is repeated:

    ([a-z]{3}).*(\1)
    

提交回复
热议问题