匹配重复与捕获组仅返回最终匹配值

时间:2016-03-16 08:37:15

标签: regex perl

我正在尝试使用匹配重复捕获组来捕获递归序列。

以下是示例文字:

some_string_here  00  12.34  34  56.78  78.90

用于捕获所有浮点值的正则表达式:

\S+(?:\s+(\d+(?:\.\d+)?)){5}

regexp按预期匹配所有浮点值,但捕获组仅返回最终匹配结果。

Group #1: 78.90

所需的结果是:

Group #1: 00
Group #2: 12.34
Group #3: 34
Group #4: 56.78
Group #5: 78.90

如果我使用以下作为正则表达式,结果是预期的,但递归序列太多,正则表达式太长。

\S+(?:\s+(\d+(?:\.\d+)?))(?:\s+(\d+(?:\.\d+)?))(?:\s+(\d+(?:\.\d+)?))(?:\s+(\d+(?:\.\d+)?))(?:\s+(\d+(?:\.\d+)?))

有没有办法捕获匹配重复中捕获组的所有浮点值?

3 个答案:

答案 0 :(得分:1)

试试这个

$s = "some_string_here  00  12.34  34  56.78  78.90";
@ar = $s =~m/(\d+\.?(?:\d+)?)/g;
$, = "\n";
print @ar;

g标志返回列表中所有可能的匹配项。列表存储在数组中。因此它将在数组中提供所有可能的匹配。

不使用g全局修饰符,它返回唯一一个00元素。因为搜索将在第一场比赛时满足。

输出

00
12.34
34 
56.78
78.90

否则,您希望存储特定数量的元素,创建列表并提供变量

例如,您只想存储三个匹配项

($first,$second,$thrid) = $s =~m/(\d+\.?(?:\d+)?)/g;

此处$first holds the 00, $second holds the 12.34 and the $third holds the 34.

答案 1 :(得分:1)

正如我在评论中提到的,你可能只想要split,就像这样

my $s = 'some_string_here  00  12.34  34  56.78  78.90';

my @groups = split ' ', $s;
shift @groups;

for my $i ( 0 .. $#groups ) {
    printf "Group #%d: %-s\n", $i+1, $groups[$i];
}

输出

Group #1: 00
Group #2: 12.34
Group #3: 34
Group #4: 56.78
Group #5: 78.90

答案 2 :(得分:0)

试试这个

(\d+\.?\d*)

Demo

输入

some_string_here  00  12.34  34  56.78  78.90

输出

MATCH 1
1.  [18-20] `00`
MATCH 2
1.  [22-27] `12.34`
MATCH 3
1.  [29-31] `34`
MATCH 4
1.  [33-38] `56.78`
MATCH 5
1.  [40-45] `78.90`
相关问题