问题描述
示例文字
outline: 4 0
corner: 1 347980000 -2540000 0
corner: 2 347980000 -20320000 0
corner: 3 482600000 -20320000 0
corner: 4 482600000 -2540000 0
outline: 4 1
corner: 1 0 -2540000 0
corner: 2 345440000 -2540000 0
corner: 3 345440000 -20320000 0
corner: 4 0 -20320000 0
outline: 8 2
corner: 1 0 0 0
corner: 2 0 35560000 0
corner: 3 53340000 35560000 0
corner: 4 53340000 76200000 0
corner: 5 449580000 76200000 0
corner: 6 449580000 30226000 0
corner: 7 482600000 30226000 0
corner: 8 482600000 0 0
outline: 4 3
corner: 1 0 38100000 0
corner: 2 50800000 38100000 0
corner: 3 50800000 76200000 0
corner: 4 0 76200000 0
outline: 4 4
corner: 1 482600000 76200000 0
corner: 2 482854000 31750000 0
corner: 3 450850000 31750000 0
corner: 4 450850000 76200000 0
/^\s+corner:\s*(\d+)\s+(-?\d+)\s+(-?\d+)\s+(\d+)/m
捕获拐角的所有值.
/^\s+corner:\s*(\d+)\s+(-?\d+)\s+(-?\d+)\s+(\d+)/m
Captures all values for corners.
/^\s*outline:\s*(\d+)\s+(\d+)$.*?\s+corner:\s*(\d+)\s+(-?\d+)\s+(-?\d+)\s+(\d+)/m
捕获所有轮廓,但仅捕获每个轮廓的第一个角.
/^\s*outline:\s*(\d+)\s+(\d+)$.*?\s+corner:\s*(\d+)\s+(-?\d+)\s+(-?\d+)\s+(\d+)/m
Captures all outlines, but only the first corner of each outline.
/^\s*outline:\s*(\d+)\s+(\d+)$.*?(^\s+corner:\s*(\d+)\s+(-?\d+)\s+(-?\d+)\s+(\d+)$).*?/m
与第二个功能相同,但是看起来像这样:
/^\s*outline:\s*(\d+)\s+(\d+)$.*?(^\s+corner:\s*(\d+)\s+(-?\d+)\s+(-?\d+)\s+(\d+)$).*?/m
Does the same thing as the second, but looks like this:
4
0
corner: 1 347980000 -2540000 0
1
347980000
-2540000
0
我正在尝试获取所有轮廓和相关角点.显然没有正确分组-有任何建议吗?
I am trying to get it to capture all of the outlines and related corners. It's obviously not properly grouped - Any suggestions?
谢谢;-)
推荐答案
由于所需的捕获数量有所不同(可能没有限制),因此您无法在一个正则表达式中执行此操作.在这种情况下String#scan
会派上用场.
Since the number of captures you want varies (probably without limit), you cannot do that in one regex. String#scan
comes in handy in such case.
text.scan(/^\s*outline:\s*(\d+)\s+(\d+)\n(.*?)(?:\n\n|\z)/m)
.map{|a, b, corners| [a, b, corners.scan(/^\s+corner:\s*(\d+)\s+(-?\d+)\s+(-?\d+)\s+(\d+)/)]}
会给您:
[["4", "0",
[["1", "347980000", "-2540000", "0"],
["2", "347980000", "-20320000", "0"],
["3", "482600000", "-20320000", "0"],
["4", "482600000", "-2540000", "0"]]],
["4", "1",
[["1", "0", "-2540000", "0"],
["2", "345440000", "-2540000", "0"],
["3", "345440000", "-20320000", "0"],
["4", "0", "-20320000", "0"]]],
["8", "2",
[["1", "0", "0", "0"],
["2", "0", "35560000", "0"],
["3", "53340000", "35560000", "0"],
["4", "53340000", "76200000", "0"],
["5", "449580000", "76200000", "0"],
["6", "449580000", "30226000", "0"],
["7", "482600000", "30226000", "0"],
["8", "482600000", "0", "0"]]],
["4", "3",
[["1", "0", "38100000", "0"],
["2", "50800000", "38100000", "0"],
["3", "50800000", "76200000", "0"],
["4", "0", "76200000", "0"]]],
["4", "4",
[["1", "482600000", "76200000", "0"],
["2", "482854000", "31750000", "0"],
["3", "450850000", "31750000", "0"],
["4", "450850000", "76200000", "0"]]]]
如果要数字而不是字符串,
If you want numbers instead of strings,
text.scan(/^\s*outline:\s*(\d+)\s+(\d+)\n(.*?)(?:\n\n|\z)/m)
.map{|a, b, corners| [a.to_i, b.to_i, corners.scan(/^\s+corner:\s*(\d+)\s+(-?\d+)\s+(-?\d+)\s+(\d+)/).map{|a| a.map(&:to_i)}]}
会给您:
[[4, 0,
[[1, 347980000, -2540000, 0],
[2, 347980000, -20320000, 0],
[3, 482600000, -20320000, 0],
[4, 482600000, -2540000, 0]]],
[4, 1,
[[1, 0, -2540000, 0],
[2, 345440000, -2540000, 0],
[3, 345440000, -20320000, 0],
[4, 0, -20320000, 0]]],
[8, 2,
[[1, 0, 0, 0],
[2, 0, 35560000, 0],
[3, 53340000, 35560000, 0],
[4, 53340000, 76200000, 0],
[5, 449580000, 76200000, 0],
[6, 449580000, 30226000, 0],
[7, 482600000, 30226000, 0],
[8, 482600000, 0, 0]]],
[4, 3,
[[1, 0, 38100000, 0],
[2, 50800000, 38100000, 0],
[3, 50800000, 76200000, 0],
[4, 0, 76200000, 0]]],
[4, 4,
[[1, 482600000, 76200000, 0],
[2, 482854000, 31750000, 0],
[3, 450850000, 31750000, 0],
[4, 450850000, 76200000, 0]]]]
这篇关于在Ruby中对正则表达式进行分组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!