首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Perl regex无法匹配行尾

Perl regex无法匹配行尾
EN

Stack Overflow用户
提问于 2014-12-11 08:29:40
回答 2查看 96关注 0票数 2

我是Perl的新手。我正在尝试提取存储在文件中的VLAN信息。文件内容,

代码语言:javascript
复制
VLAN0001
  Spanning tree enabled protocol rstp

Interface           Role Sts Cost      Prio.Nbr Type
------------------- ---- --- --------- -------- --------------------------------
PE8/1             Desg FWD 2         128.2945 P2p Edge 
Ta579               Desg FWD 3         128.5761 P2p Edge 

VLAN0023
  Spanning tree enabled protocol rstp

Interface           Role Sts Cost      Prio.Nbr Type
------------------- ---- --- --------- -------- --------------------------------
PE8/1             Desg FWD 2         128.2945 P2p Edge 
Ta579               Desg FWD 3         128.5761 P2p Edge    

ACCOUNT
  Spanning tree enabled protocol rstp

Interface           Role Sts Cost      Prio.Nbr Type
------------------- ---- --- --------- -------- --------------------------------
Ta579               Desg FWD 1         128.5764 P2p

我有perl代码,

代码语言:javascript
复制
#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper;
my $filename = "spanning-tree1.txt";
    open my $fh, '<', $filename or die "error opening $filename: $!";
    my $data = do { local $/; <$fh> };

my @list = ($data =~ /(^[A-Za-z0-9]+.*?(?=^[A-Za-z0-9]+$|\Z))/msg);
#print Dumper($data);
#print "\n##############################################\n";
print Dumper(\@list);

它的输出是

代码语言:javascript
复制
$VAR1 = [
          'VLAN0001
  Spanning tree enabled protocol rstp

Interface           Role Sts Cost      Prio.Nbr Type
------------------- ---- --- --------- -------- --------------------------------
PE8/1             Desg FWD 2         128.2945 P2p Edge
Ta579               Desg FWD 3         128.5761 P2p Edge

VLAN0023
  Spanning tree enabled protocol rstp

Interface           Role Sts Cost      Prio.Nbr Type
------------------- ---- --- --------- -------- --------------------------------
PE8/1             Desg FWD 2         128.2945 P2p Edge
Ta579               Desg FWD 3         128.5761 P2p Edge

ACCOUNT
  Spanning tree enabled protocol rstp

Interface           Role Sts Cost      Prio.Nbr Type
------------------- ---- --- --------- -------- --------------------------------
Ta579               Desg FWD 1         128.5764 P2p'
        ];

我需要输出(@list)

代码语言:javascript
复制
$VAR1 = [
          'VLAN0001
  Spanning tree enabled protocol rstp

Interface           Role Sts Cost      Prio.Nbr Type
------------------- ---- --- --------- -------- --------------------------------
PE8/1             Desg FWD 2         128.2945 P2p Edge
Ta579               Desg FWD 3         128.5761 P2p Edge

',
          'VLAN0023
  Spanning tree enabled protocol rstp

Interface           Role Sts Cost      Prio.Nbr Type
------------------- ---- --- --------- -------- --------------------------------
PE8/1             Desg FWD 2         128.2945 P2p Edge
Ta579               Desg FWD 3         128.5761 P2p Edge

',
          'ACCOUNT
  Spanning tree enabled protocol rstp

Interface           Role Sts Cost      Prio.Nbr Type
------------------- ---- --- --------- -------- --------------------------------
Ta579               Desg FWD 1         128.5764 P2p'
        ];

有趣的是,我在@list 中得到了适当的值,当输入作为字符串给出,而不是从文件中读取。。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-12-11 10:37:33

你可以用数字和大写字母在行的开头分割整串,

代码语言:javascript
复制
my @list = split /(?= ^[A-Z0-9]+\s*$ )/mx, do { local $/; <DATA> };
票数 2
EN

Stack Overflow用户

发布于 2014-12-11 10:18:29

有时regex并不是唯一的解决方案:

代码语言:javascript
复制
#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper;

my $inc = -1;
my @list;

my $filename = "spanning-tree1.txt";
open my $fh, '<', $filename or die "error opening $filename: $!";

while (<$fh>) {
    /^[A-Z0-9]+\s*$/ and $inc++;
    $list[$inc] .= $_;
}
print Dumper(\@list);
close $fh;
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27418365

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档