通过索引perl将字符串拆分为多个部分

时间:2011-09-26 13:44:04

标签: perl

我有一个diskpart命令的输出:

Volume ###  Ltr  Label        Fs     Type        Size     Status     Info
----------  ---  -----------  -----  ----------  -------  ---------  --------
Volume 0     D                       DVD-ROM         0 B  No Media
Volume 1     C   OSDisk       NTFS   Partition    232 GB  Healthy    Boot
Volume 2         BDEDrive     NTFS   Partition    300 MB  Healthy    System

我想将每一个捕获到他们自己的特定变量中,所以我的第一个倾向是做一些像($ volume,$ ltr,...,$ info)= $ line =〜((\ w + \ s) \ d +)\ s +([AZ])?...

我遇到的问题是Label,FS和Type之间没有任何区别,所以如果我在每个列上都使用(\w+)\s+,那么标签可能不存在而是FS是的,因此文件系统不正确地读入$ label。

我不太确定我是否可以使用正则表达式来完成这项工作,但我愿意接受建议!相反,我会朝着一个新方向前进,然后根据开始 - 和结束 - 的指数分割字符串。如果我提取了所有这些索引,那么将这个字符串分成各自的子字符串Perl的最佳方法是什么?

我查看了substr,并尝试将多个索引传递给($a,$b,$c) = substr('abcd', 1,2,3);,但这只会导致$ a在2,3之间分割

除了一次只拆分一行之外,还有什么优雅的解决方案吗?

2 个答案:

答案 0 :(得分:5)

而不是使用(不是非常可维护的)正则表达式,使用unpack更容易:

my @l = unpack('A12 A5 A13 A7 A12 A9 A11 A9', $_);

你仍然要抛弃第二行,但你不必关心数据的样子。

答案 1 :(得分:2)

怎么样:

#!/usr/bin/perl
use strict;
use warnings;
use Data::Dump qw(dump);


while(<DATA>) {
    chomp;
    my @l = /^(\w*\s\d*)\s+(\w|\s)\s+(\w+|\s+)\s+(\w+|\s+)\s+([\w-]+|\s+)\s+(\d+\s\w{1,2})\s+?([\w\s]+)\s+?([\w\s]+)$/;
    dump(@l) if @l;
}


__DATA__
Volume ###  Ltr  Label        Fs     Type        Size     Status     Info
----------  ---  -----------  -----  ----------  -------  ---------  --------
Volume 0     D                       DVD-ROM         0 B  No Media          
Volume 1     C   OSDisk       NTFS   Partition    232 GB  Healthy    Boot
Volume 2         BDEDrive     NTFS   Partition    300 MB  Healthy    System

<强>输出:

(
  "Volume 0",
  "D",
  " ",
  " ",
  "DVD-ROM",
  "0 B",
  " No Media        ",
  " ",
)

(
  "Volume 1",
  "C",
  "OSDisk",
  "NTFS",
  "Partition",
  "232 GB",
  " Healthy   ",
  "Boot",
)

(
  "Volume 2",
  " ",
  "BDEDrive",
  "NTFS",
  "Partition",
  "300 MB",
  " Healthy   ",
  "System",
)