【发布时间】:2013-02-28 13:39:59
【问题描述】:
我的数据:
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+))+)+$
这根本不起作用(为什么?),所以我删除了 ^ 和 $ 并且它匹配。然后比赛看起来像这样:
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: 组合?
【问题讨论】:
-
你必须更深入!
标签: regex