我的数据:
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
我似乎缺少匹配一个或多个(?: - m: (\d+) t: (\d+))+
组合的内部组“13、14”。
在线测试: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,我认为我的问题与此有关?任何提示/指针/帮助,以便我可以匹配一个或多个 m:t: 组合?