正则表达式-捕获所有重复的迭代 [英] Regex - capture all repeated iteration

查看:62
本文介绍了正则表达式-捕获所有重复的迭代的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个像这样的变量

var = "!123abcabc123!"

我正在尝试捕获此变量中的所有 123和 abc。

i'm trying to capture all the '123' and 'abc' in this var.

此正则表达式(abc | 123)检索我想要但是......

this regex (abc|123) retrieve what i want but...

我的问题是:当我尝试此正则表达式!(abc | 123)*!时,它仅检索最后一次迭代。我将如何获得此输出

My question is: when i try this regex !(abc|123)*! it retrieve only the last iteration. what will i do to get this output

MATCH 1
1.  [1-4]   `123`
MATCH 2
1.  [4-7]   `abc`
MATCH 3
1.  [7-10]  `abc`
MATCH 4
1.  [10-13] `123`

https://regex101.com/r/mD4vM8/3

谢谢!

推荐答案

如果您的语言支持<$​​ c $ c> \G ,则可以免费使用。

If your language supports \G then you may free to use this.

(?:!|\G(?!^))\K(abc|123)(?=(?:abc|123)*!)

演示

这篇关于正则表达式-捕获所有重复的迭代的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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