Match at every second occurrence

前端 未结 6 1619
我在风中等你
我在风中等你 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:04

    There's no "direct" way of doing so but you can specify the pattern twice as in: a[^a]*a that match up to the second "a".

    The alternative is to use your programming language (perl? C#? ...) to match the first occurence and then the second one.

    EDIT: I've seen other responded using the "non-greedy" operators which might be a good way to go, assuming you have them in your regex library!

提交回复
热议问题