简体   繁体   English

Perl正则表达式-动态匹配

[英]Perl Regular Expression - Dynamic Matches

Let's say I want to match and capture 5 integers separated by one or more spaces - example input: 假设我要匹配并捕获5个由一个或多个空格分隔的整数-示例输入:

1111 234 3333 456 7890 1111 234 3333 456 7890

I could do this: 我可以这样做:

my $input = '1111        234            3333          456    7890';
if($input =~ /^\s*([0-9]+)\s+([0-9]+)\s+([0-9]+)\s+([0-9]+)\s+([0-9]+)/)
{
  #$1 = '1111', $2 = '234', $3 = '3333', $4= '456', $5 = '7890'
}

But I want to do something like this to keep the regex simpler, rather than repeating each int 5 times explicitly: 但是我想做这样的事情来使正则表达式更简单,而不是明确地将每个int重复5次:

my $input = '1111        234            3333          456    7890';
if($input =~ /^((\s*[0-9]+){5})/)
{
  #$1 = '1111        234            3333          456    7890';
  #$2 = ' 7890'
  #all other capture variables are undefined
}

However, the captures don't seem to work out. 但是,捕获似乎无法解决。

Is there a way I can I do this and still access my 5 captures? 有没有办法我仍然可以访问我的5个照片?

Even better would be an unknown number of captures: 更好的是捕获数量未知:

my $input = '1111        234            3333          456    7890';
if($input =~ /^((\s*[0-9]+)+)/)
{
   #foreach capture 1..N do something...
}
my @numbers = $input =~ /\d+/g;

全局标志将返回列表上下文中的所有匹配项,这些匹配项将存储在您的数组中。

If you know what your delimiter is (in this case, one or more spaces), then you don't need a regex to capture what you want. 如果知道分隔符是什么(在本例中为一个或多个空格),则不需要正则表达式即可捕获所需的内容。 You can use split . 您可以使用split

use strict;
use warnings;

my $input = "1111        234            3333          456    7890";
my @ints=split /\s+/,$input;
print "$_\n" foreach(@ints);

Which produces the output: 产生输出:

1111
234
3333
456
7890

Is the pattern of the line always digit groups separated by spaces? 线条的模式是否总是用空格分隔数字组? If so, rather than the regex, why not split into array based on whitespace 如果是这样,而不是正则表达式,为什么不基于空格拆分成数组

@outArray = split (/ +/,$input);

The following would capture the first 5 integers and ignore any after that if thats what you're after. 下面的代码将捕获前5个整数,如果这就是您想要的,则忽略其后的任何整数。 I may not be entirely clear. 我可能不太清楚。

#!/usr/bin/perl
use strict;
use warnings;

my $in = '1111        234            3333          456    7890 12 13';

my @ints = (split ' ', $in)[0 .. 4];

print "@ints\n";

Prints: 印刷品:

1111 234 3333 456 7890

Chris 克里斯

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM