Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex制作智能数据_Regex_Perl - Fatal编程技术网

Regex制作智能数据

Regex制作智能数据,regex,perl,Regex,Perl,我绞尽脑汁,试图想出一个正则表达式,它能够在这个智能数据输出中提取我想要的数据: 脱机数据采集状态:(0x00)脱机数据采集活动 从来没有开始过。 自动脱机数据收集:已禁用。 自检执行状态:(0)上一个自检例程已完成 没有错误或没有自我测试 已经运行了。 脱机完成的总时间 数据收集:(139)秒。 离线数据收集 功能:(0x73)智能脱机立即执行。 自动脱机数据采集开/关支持。 在收到新邮件时暂停脱机收集 指挥部。 不支持脱机表面扫描。 支持自检。 支持传输自检。 支持选择性自检。 智能功能:(

我绞尽脑汁,试图想出一个正则表达式,它能够在这个智能数据输出中提取我想要的数据:

脱机数据采集状态:(0x00)脱机数据采集活动
从来没有开始过。
自动脱机数据收集:已禁用。
自检执行状态:(0)上一个自检例程已完成
没有错误或没有自我测试
已经运行了。
脱机完成的总时间
数据收集:(139)秒。
离线数据收集
功能:(0x73)智能脱机立即执行。
自动脱机数据采集开/关支持。
在收到新邮件时暂停脱机收集
指挥部。
不支持脱机表面扫描。
支持自检。
支持传输自检。
支持选择性自检。
智能功能:(0x0003)在输入前保存智能数据
省电模式。
支持智能自动保存计时器。
错误记录功能:(0x01)支持错误记录。
支持通用日志记录。
短自检程序
建议的轮询时间:(2)分钟。
扩展自检程序
建议轮询时间:(100)分钟。
运输自测程序
建议轮询时间:(3)分钟。
SCT功能:(0x1081)支持SCT状态。
到目前为止,我提出的正则表达式是:

/([^A-Za-z]?:)([\w\s\/().\-]+\.)/gm
我的正则表达式的目标是从
smartctl-a
输出中获取每个“通用智能值”的“值”。问题是输出的格式是特殊的,这使得我很难将想要的值拉入数组

我能够只拉智能值键,如
离线数据采集状态
,或
自检执行状态
,因此现在我正在拉取这些参数的值。类似于
(139)秒
(0x00)的脱机数据收集活动从未启动。

键和值之间的区别是这个冒号后跟一些空格。但是,在其中一个值中,它包含的文本中也有冒号,这使得捕获非常困难。我需要捕获以下所有内容,而不会意外捕获下一个参数值

脱机数据采集状态:(0x00)脱机数据采集活动
从来没有开始过。
自动脱机数据收集:已禁用。
自检执行状态:(0)上一个自检例程已完成
没有错误或没有自我测试
已经运行了。
脱机完成的总时间
数据收集:(139)秒。
因此,从上面我需要捕捉以下内容

(0x00)脱机数据收集活动
从来没有开始过。
自动脱机数据收集:已禁用。
无需进入并捕获
自检执行状态:
作为其一部分,因为这是下一个参数键


对这种情况的任何想法都会有帮助。

我认为你可以利用这样一个事实,即关键点从一开始就开始了
行的长度和值始终至少有一个水平空白
在每一个之前

(?:^(?!\s)[^:\n]*\n?+:(\h+.*(?:\n |\z)(?:^\h+.*(?:\n |\z))*)?

不需要修改器,包括在内

while ( $smartdata =~ /(?m)((?:^(?!\s)[^:\n]*\n?)+):(\h+.*?(?:\n|\z)(?:^\h+.*?(?:\n|\z))*)?/g )
{
    push @key, $1;
    push @value, $2;
}
扩大

 (?m)
 (                             # (1 start), Key
      (?:
           ^ 
           (?! \s )
           [^:\n]* 
           \n? 
      )+
 )                             # (1 end)
 : 
 (                             # (2 start), Value
      \h+ .*?  
      (?: \n | \z )
      (?:
           ^ \h+ .*?  
           (?: \n | \z )
      )*
 )?                            # (2 end)
Perl示例

use strict;
use warnings;

$/ = undef;

my $smartdata = <DATA>;

my @key = ();
my @val = ();

while ( $smartdata =~ /(?m)((?:^(?!\s)[^:\n]*\n?)+):(\h+.*?(?:\n|\z)(?:^\h+.*?(?:\n|\z))*)?/g )
{
    push @key, $1;
    if (defined $2 ) {
        push @val, $2;
    }
    else {
        push @val, '';
    }
}

for ( 0 .. ($#key-1) )
{
     print "key $_ = $key[$_]\n";
     print "value = $val[$_]\n-------------------\n";
}

__DATA__

Offline data collection status:  (0x00) Offline data collection activity
                    was never started.
                    Auto Offline Data Collection: Disabled.
Self-test execution status:      (   0) The previous self-test routine completed
                    without error or no self-test has ever 
                    been run.
Total time to complete Offline 
data collection:        (  139) seconds.
Offline data collection
capabilities:            (0x73) SMART execute Offline immediate.
                    Auto Offline data collection on/off support.
                    Suspend Offline collection upon new
                    command.
                    No Offline surface scan supported.
                    Self-test supported.
                    Conveyance Self-test supported.
                    Selective Self-test supported.
SMART capabilities:            (0x0003) Saves SMART data before entering
                    power-saving mode.
                    Supports SMART auto save timer.
Error logging capability:        (0x01) Error logging supported.
                    General Purpose Logging supported.
Short self-test routine 
recommended polling time:    (   2) minutes.



Extended self-test routine
recommended polling time:    ( 100) minutes.
Conveyance self-test routine
recommended polling time:    (   3) minutes.
SCT capabilities:          (0x1081) SCT Status supported.

我认为您可以利用这样一个事实,即按键从一开始就开始
行的长度和值始终至少有一个水平空白
在每一个之前

(?:^(?!\s)[^:\n]*\n?+:(\h+.*(?:\n |\z)(?:^\h+.*(?:\n |\z))*)?

不需要修改器,包括在内

while ( $smartdata =~ /(?m)((?:^(?!\s)[^:\n]*\n?)+):(\h+.*?(?:\n|\z)(?:^\h+.*?(?:\n|\z))*)?/g )
{
    push @key, $1;
    push @value, $2;
}
扩大

 (?m)
 (                             # (1 start), Key
      (?:
           ^ 
           (?! \s )
           [^:\n]* 
           \n? 
      )+
 )                             # (1 end)
 : 
 (                             # (2 start), Value
      \h+ .*?  
      (?: \n | \z )
      (?:
           ^ \h+ .*?  
           (?: \n | \z )
      )*
 )?                            # (2 end)
Perl示例

use strict;
use warnings;

$/ = undef;

my $smartdata = <DATA>;

my @key = ();
my @val = ();

while ( $smartdata =~ /(?m)((?:^(?!\s)[^:\n]*\n?)+):(\h+.*?(?:\n|\z)(?:^\h+.*?(?:\n|\z))*)?/g )
{
    push @key, $1;
    if (defined $2 ) {
        push @val, $2;
    }
    else {
        push @val, '';
    }
}

for ( 0 .. ($#key-1) )
{
     print "key $_ = $key[$_]\n";
     print "value = $val[$_]\n-------------------\n";
}

__DATA__

Offline data collection status:  (0x00) Offline data collection activity
                    was never started.
                    Auto Offline Data Collection: Disabled.
Self-test execution status:      (   0) The previous self-test routine completed
                    without error or no self-test has ever 
                    been run.
Total time to complete Offline 
data collection:        (  139) seconds.
Offline data collection
capabilities:            (0x73) SMART execute Offline immediate.
                    Auto Offline data collection on/off support.
                    Suspend Offline collection upon new
                    command.
                    No Offline surface scan supported.
                    Self-test supported.
                    Conveyance Self-test supported.
                    Selective Self-test supported.
SMART capabilities:            (0x0003) Saves SMART data before entering
                    power-saving mode.
                    Supports SMART auto save timer.
Error logging capability:        (0x01) Error logging supported.
                    General Purpose Logging supported.
Short self-test routine 
recommended polling time:    (   2) minutes.



Extended self-test routine
recommended polling time:    ( 100) minutes.
Conveyance self-test routine
recommended polling time:    (   3) minutes.
SCT capabilities:          (0x1081) SCT Status supported.

键和数据都是跨行拆分的,因此我们必须处理这两种情况:

use strict;
use warnings;

my %data;

my $lastkey;

my $prefixkey = "";

while (my $smartdata = <DATA>) {
    chomp $smartdata;

    if ($smartdata =~ m/^\S/) {
        if ($smartdata =~ m/^([^:]+):\s+(.*)$/) { # is a complete or end of a key and data

            $lastkey = $prefixkey ? "$prefixkey $1" : $1;

            $data{$lastkey} = $2;

            $prefixkey = "";
        }
        else { # this is the start of a key
            $smartdata =~ s/(^\s+|\s+$)//; # strip whitespace
            $prefixkey = $smartdata;
        }
    }   
    else { # this is a data continuation
        $smartdata =~ s/(^\s+|\s+$)//; # strip whitespace
        $data{$lastkey} .= " $smartdata";
    }
}

for my $key (keys(%data)) {
    print("$key:\t$data{$key}\n");
}

__DATA__
Offline data collection status:  (0x00) Offline data collection activity
                    was never started.
                    Auto Offline Data Collection: Disabled.
Self-test execution status:      (   0) The previous self-test routine completed
                    without error or no self-test has ever 
                    been run.
Total time to complete Offline 
data collection:        (  139) seconds.
Offline data collection
capabilities:            (0x73) SMART execute Offline immediate.
                    Auto Offline data collection on/off support.
                    Suspend Offline collection upon new
                    command.
                    No Offline surface scan supported.
                    Self-test supported.
                    Conveyance Self-test supported.
                    Selective Self-test supported.
SMART capabilities:            (0x0003) Saves SMART data before entering
                    power-saving mode.
                    Supports SMART auto save timer.
Error logging capability:        (0x01) Error logging supported.
                    General Purpose Logging supported.
Short self-test routine 
recommended polling time:    (   2) minutes.
Extended self-test routine
recommended polling time:    ( 100) minutes.
Conveyance self-test routine
recommended polling time:    (   3) minutes.
SCT capabilities:          (0x1081) SCT Status supported.

键和数据都是跨行拆分的,因此我们必须处理这两种情况:

use strict;
use warnings;

my %data;

my $lastkey;

my $prefixkey = "";

while (my $smartdata = <DATA>) {
    chomp $smartdata;

    if ($smartdata =~ m/^\S/) {
        if ($smartdata =~ m/^([^:]+):\s+(.*)$/) { # is a complete or end of a key and data

            $lastkey = $prefixkey ? "$prefixkey $1" : $1;

            $data{$lastkey} = $2;

            $prefixkey = "";
        }
        else { # this is the start of a key
            $smartdata =~ s/(^\s+|\s+$)//; # strip whitespace
            $prefixkey = $smartdata;
        }
    }   
    else { # this is a data continuation
        $smartdata =~ s/(^\s+|\s+$)//; # strip whitespace
        $data{$lastkey} .= " $smartdata";
    }
}

for my $key (keys(%data)) {
    print("$key:\t$data{$key}\n");
}

__DATA__
Offline data collection status:  (0x00) Offline data collection activity
                    was never started.
                    Auto Offline Data Collection: Disabled.
Self-test execution status:      (   0) The previous self-test routine completed
                    without error or no self-test has ever 
                    been run.
Total time to complete Offline 
data collection:        (  139) seconds.
Offline data collection
capabilities:            (0x73) SMART execute Offline immediate.
                    Auto Offline data collection on/off support.
                    Suspend Offline collection upon new
                    command.
                    No Offline surface scan supported.
                    Self-test supported.
                    Conveyance Self-test supported.
                    Selective Self-test supported.
SMART capabilities:            (0x0003) Saves SMART data before entering
                    power-saving mode.
                    Supports SMART auto save timer.
Error logging capability:        (0x01) Error logging supported.
                    General Purpose Logging supported.
Short self-test routine 
recommended polling time:    (   2) minutes.
Extended self-test routine
recommended polling time:    ( 100) minutes.
Conveyance self-test routine
recommended polling time:    (   3) minutes.
SCT capabilities:          (0x1081) SCT Status supported.

这些数据的格式不是最好的,但至少是可以预测的。我们可以根据每行开头的样子来解析它

use strict;
use warnings;
use Data::Dumper;

my %data;
my $key;
my $record;

while (<DATA>) {
    chomp;

    if (s/^\s+/ /g) {
        $record .= $_;
    } elsif (s/^([^:]+):\s\s+//) {
        if (length($record)) {
            $data{$key} = $record;
            $key = '';
        }

        $key .= $1;
        $record = $_;
    } else {
        $data{$key} = $record;
        $key = $_ . ' ';
        $record = '';
    }
}

$data{$key} = $record;
print Dumper(\%data);

__DATA__
Offline data collection status:  (0x00) Offline data collection activity
                    was never started.
                    Auto Offline Data Collection: Disabled.
Self-test execution status:      (   0) The previous self-test routine completed
                    without error or no self-test has ever
                    been run.
Total time to complete Offline
data collection:        (  139) seconds.
Offline data collection
capabilities:            (0x73) SMART execute Offline immediate.
                    Auto Offline data collection on/off support.
                    Suspend Offline collection upon new
                    command.
                    No Offline surface scan supported.
                    Self-test supported.
                    Conveyance Self-test supported.
                    Selective Self-test supported.
SMART capabilities:            (0x0003) Saves SMART data before entering
                    power-saving mode.
                    Supports SMART auto save timer.
Error logging capability:        (0x01) Error logging supported.
                    General Purpose Logging supported.
Short self-test routine
recommended polling time:    (   2) minutes.
Extended self-test routine
recommended polling time:    ( 100) minutes.
Conveyance self-test routine
recommended polling time:    (   3) minutes.
SCT capabilities:          (0x1081) SCT Status supported.

这些数据的格式不是最好的,但至少是可以预测的。我们可以根据每行开头的样子来解析它

use strict;
use warnings;
use Data::Dumper;

my %data;
my $key;
my $record;

while (<DATA>) {
    chomp;

    if (s/^\s+/ /g) {
        $record .= $_;
    } elsif (s/^([^:]+):\s\s+//) {
        if (length($record)) {
            $data{$key} = $record;
            $key = '';
        }

        $key .= $1;
        $record = $_;
    } else {
        $data{$key} = $record;
        $key = $_ . ' ';
        $record = '';
    }
}

$data{$key} = $record;
print Dumper(\%data);

__DATA__
Offline data collection status:  (0x00) Offline data collection activity
                    was never started.
                    Auto Offline Data Collection: Disabled.
Self-test execution status:      (   0) The previous self-test routine completed
                    without error or no self-test has ever
                    been run.
Total time to complete Offline
data collection:        (  139) seconds.
Offline data collection
capabilities:            (0x73) SMART execute Offline immediate.
                    Auto Offline data collection on/off support.
                    Suspend Offline collection upon new
                    command.
                    No Offline surface scan supported.
                    Self-test supported.
                    Conveyance Self-test supported.
                    Selective Self-test supported.
SMART capabilities:            (0x0003) Saves SMART data before entering
                    power-saving mode.
                    Supports SMART auto save timer.
Error logging capability:        (0x01) Error logging supported.
                    General Purpose Logging supported.
Short self-test routine
recommended polling time:    (   2) minutes.
Extended self-test routine
recommended polling time:    ( 100) minutes.
Conveyance self-test routine
recommended polling time:    (   3) minutes.
SCT capabilities:          (0x1081) SCT Status supported.

仅供参考,您正在regex.com表达式中使用Python正则表达式。我认为在这种情况下没有什么不同,但是Python正则表达式!=Perl正则表达式。我知道在那个站点上是python正则表达式,我没有看到Perl的选项,但是正如您所说的,这里可能没有太大的区别,因为正则表达式应该是通用的。如果你有一个网站会做同样的事情,但是会显示Perl正则表达式,请告诉我。@HuyNguyen我会使用PCRE来显示PerlPCRE,虽然它们仍然不相等。谢谢你的建议,但是我认为我们正在失去我问题的焦点。我只是用regex101作为一种直观的更简单的方式来描述我所面临的问题,并在regex表达式制作方面寻求建议,因为我对此并不感到惊讶。提供的任何和所有建议,我都可以针对一个perl脚本进行测试,我正在运行该脚本来提取我要查找的数据,我只是觉得划分要显示的正则表达式部分是有帮助的。仅供参考,您在regex.com表达式中使用的是Python正则表达式。我认为在这种情况下没有什么不同,但是Python正则表达式!=Perl正则表达式。我知道在那个站点上是python正则表达式,我没有看到Perl的选项,但是正如您所说的,这里可能没有太大的区别,因为正则表达式应该是通用的。如果你有一个网站会做同样的事情,但是会显示Perl正则表达式,请告诉我。@HuyNguyen我会使用PCRE来显示PerlPCRE,但是会更接近