每隔一次匹配一次 [英] Match at every second occurrence

查看:63
本文介绍了每隔一次匹配一次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法指定一个正则表达式来匹配字符串中每第二次出现的模式?

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

示例

  • 针对字符串 abcdabcd 搜索 a 应该在位置 5 处找到一个匹配项
  • 针对字符串 abcdabcd 搜索 ab 应该在位置 5 处找到一个匹配项
  • 针对字符串 abcdabcd 搜索 dab 应该找不到匹配项
  • 针对字符串aaaa搜索a应该会在位置2和4处找到两次
  • searching for a against string abcdabcd should find one occurrence at position 5
  • searching for ab against string abcdabcd should find one occurrence at position 5
  • searching for dab against string abcdabcd should find no occurrences
  • searching for a against string aaaa should find two occurrences at positions 2 and 4

推荐答案

使用分组.

foo.*?(foo)

这篇关于每隔一次匹配一次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆