My data:
stack: 123 overflow: 456 others: - st: 7 ov: 7 againothers: - m: 11 t: 12 - m: 13 t: 14 - m: 15 t: 16 - st: 8 ov: 8 againothers: - m: 17 t: 18 end: 42
我的正则表达式:
^stack: (\d+) overflow: (\d+) others: ?(.+) end: (\d+)$
其匹配组为:
1: 123
2: 456
3: - st: 7 ov: 7 againothers: - m: 11 t: 12 - m: 13 t: 14 - m: 15 t: 16 - st: 8 ov: 8 againothers: - m: 17 t: 18
4: 42
目前很好。然后在组 3 上运行以下正则表达式:
^(?:- st: (\d+) ov: (\d+) againothers: ?(?: - m: (\d+) t: (\d+))+)+$
这根本不起作用(为什么?),所以我删除了^
and $
它匹配。比赛看起来像这样:
1: 7 // <-- Works as expected.
2: 7
3: 15 // <-- Here I'd expected 2 groups matching: (13,14), (15,16)
4: 16 // <-- but I'm only getting the last group.
1: 8 // <-- This works and the remainder is as expected.
2: 8
3: 17
4: 18
我似乎缺少与一个或多个匹配的内部组“13, 14”(?: - m: (\d+) t: (\d+))+
组合。
在线测试:http://gskinner.com/RegExr/?33urf http://gskinner.com/RegExr/?33urf,以防被屠杀,我的数据是:- st: 7 ov: 7 againothers: - m: 11 t: 12 - m: 13 t: 14 - m: 15 t: 16 - st: 8 ov: 8 againothers: - m: 17 t: 18
正则表达式是:(?:- st: (\d+) ov: (\d+) againothers: ?(?: - m: (\d+) t: (\d+))+)+
.
我读了http://www.regular-expressions.info/captureall.html http://www.regular-expressions.info/captureall.html,我认为我的问题与此有关?有任何提示/指示/帮助以便我可以匹配一个或多个 m:t: 组合吗?