RegEx在字符串中替换了多个捕获组

时间:2014-12-02 14:41:44

标签: regex capture-group

我有一串数据......

"123456712J456","D","TEST1~TEST2~TEST3~TEST4~TEST5"

我想采用以下字符串并制作5个字符串。

"123456712J456","D","TEST1"
"123456712J456","D","TEST2"
"123456712J456","D","TEST3"
"123456712J456","D","TEST4"
"123456712J456","D","TEST5"

我目前有以下正则表达式...

//In a program like Textpad
<FIND> "\(.\{13\}\)","D","\([^~]*\)~\(.*\)
<REPLACE> "\1","D","\2"\n"\1","D","\3

//On the regex101 site
"(.{13})","D","([^~]*)~(.*)

现在,如果我运行5次,它会正常工作。问题是要生成未知数量的行。例如......

"123456712J456","D","TEST1~TEST2~TEST3~TEST4~TEST5"
"123456712J457","D","TEST1~TEST2~TEST3"
"123456712J458","D","TEST1~TEST2"
"123456712J459","D","TEST1~TEST2~TEST3~TEST4"

我希望能够使用MULTI捕获组来完成这项工作。我发现这个PAGE谈论重复捕获组和捕获重复组之间的常见错误。我需要抓住一个重复的小组。出于某种原因,我不能让我的工作正常。其他人有想法吗?


资源:

1 个答案:

答案 0 :(得分:1)

试试这个。看看demo.Just club match1rest of the matches

http://regex101.com/r/yR3mM3/17

正则表达式:

(.*,)|([^"~]+)

示例:

"1234567123456","T","TEST1~TEST2~TEST3~TEST4~TEST5"

结果:

MATCH 1
  1.    [0-20]  `"1234567123456","T",`

MATCH 2
  2.    [21-26] `TEST1`

MATCH 3
  2.    [27-32] `TEST2`

MATCH 4
  2.    [33-38] `TEST3`

MATCH 5
  2.    [39-44] `TEST4`

MATCH 6
  2.    [45-50] `TEST5`