如何定义一个正则表达式,使4、3或2个单词与该分层顺序相匹配



更准确地说,我需要一个正则表达式来匹配3、2或1个连续的大写单词和2到5位数字。。。并将每个单词和数字保存在捕获组中。。。例如:

BULDING ROBERT SMITH 362 ---> Should be matched and the following should 
                              be valid: $1="BULDING"; $2="ROBERT"; $3="SMITH"; $4="362";
BULDING STEVENSON 7255 ---> Should be matched and the following should 
                              be valid: $1="BULDING"; $2="STEVENSON"; $3="7255";
BULDING 15 ---> Should be matched and the following should 
                              be valid: $1="BULDING"; $2="15";

直到现在,我想出了以下

([A-Z]+ )?([A-Z]+ )?([A-Z]+) b(d{2,5})b

但不能满足我的需求,因为它还匹配第一个和第二个可选匹配之后的"。。。你能帮忙拿吗?

只需两步即可完成:

#!/usr/bin/env perl
use strict;
use warnings;
use v5.10;
while (<DATA>) {
    if (/b((?:[A-Z]+s+){1,3})b(d+)b/) {
        my @words = split ' ', $1;
        my $num = $2;
        say "Words = " . join ', ', @words;
        say "Num   = $num";
    }
}
__DATA__
BULDING ROBERT SMITH 362
BULDING STEVENSON 7255
BULDING 15

输出:

Words = BULDING, ROBERT, SMITH
Num   = 362
Words = BULDING, STEVENSON
Num   = 7255
Words = BULDING
Num   = 15

不要占用空间。使用非捕获组进行可选:

(?:([A-Z]+) )?(?:([A-Z]+) )?([A-Z]+) b(d{2,5})b

(?:...)创建了一个非捕获组,该组用于将表达式括起来,但不会在匹配结果中创建组。

ans:

 ([A-Z]+) ([A-Z]+)? ([A-Z]+)? (d{2,5})

这种普通的快递可以让人饱腹我认为您可以使用split()函数;

use strict;
use 5.010;
my $str="BULDING ROBERT SMITH 362";
my @array = split(" ",$str);
my $num = pop(@array);
my ($str1,$str2,$str3) = @array;
say $str1;
say $str2 if $str2;
say $str3 if $str3; 

最新更新