Regex - capture all repeated iteration

女生的网名这么多〃 提交于 2019-12-20 04:48:53

问题


I have a variable like this

var = "!123abcabc123!"

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

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

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

Thank you!!


回答1:


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

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

DEMO



来源:https://stackoverflow.com/questions/31992558/regex-capture-all-repeated-iteration

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!