我正绞尽脑汁试图想出一个能够在SMART数据输出中提取我想要的数据的正则表达式:
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.
到目前为止我提出的正则表达式是:
/([^A-Za-z]?:)([\w\s\/().\-]+\.)/gm
我的正则表达式的目标是从smartctl -a
输出中获取每个“常规SMART值”的“值”。问题是输出的格式是特定的,这使得我很难将我想要的值拉入数组。
我只能提取Offline data collection status
或Self-test execution status
等SMART值键,所以现在我正在努力提取每个参数的值。这可能是(139) seconds
或(0x00) Offline data collection activity was never started.
键与值的区别在于冒号后跟一些空格。但是在其中一个值中,它包含的文本中也包含一个冒号,这使得捕获非常困难。我需要捕获以下所有内容而不会意外捕获下一个参数值。
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.
所以从上面我需要捕获以下内容。
(0x00) Offline data collection activity
was never started.
Auto Offline Data Collection: Disabled.
不进入并捕获Self-test execution status:
作为其中一部分,因为那是下一个参数键。
对这种情况的任何帮助都会有所帮助。
答案 0 :(得分:2)
我认为你可以利用钥匙从头开始的事实
线的值和值总是至少有一个水平的空格
在每一个之前。
> print(cols)
setosa.Yellow versicolor.Blue virginica.Purple
"#F6B436" "#4D86A0" "#672767"
不需要包含修饰符。
# This works, though
cols2 <- c("setosa" = "#F6B436",
"versicolor" = "#4D86A0",
"virginica" = "#672767")
ggplot(iris, aes(x = Sepal.Width, y = Sepal.Length, col = Species)) +
geom_point(size=5) +
scale_color_manual(values=cols2)
扩展
Button guardar;
Button volver;
EditText name;
EditText email;
EditText password;
EditText age;
EditText address;
EditText phone;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_registro);
guardar = (Button) findViewById(R.id.guardar);
guardar.setOnClickListener(this);
volver = (Button) findViewById(R.id.volver);
volver.setOnClickListener(this);
}
@Override
public void onClick (View v){
switch (v.getId()) {
case R.id.guardar:
Toast.makeText(Registro.this, "¡Los datos se han guardado con éxito!", Toast.LENGTH_LONG).show();
startActivity(new Intent(this, Casa.class));
break;
case R.id.volver:
startActivity(new Intent(this, MainActivity.class));
break;
}
}
Perl样本
(?m)((?:^(?!\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;
}
答案 1 :(得分:1)
密钥和数据都是按行拆分的,因此我们必须处理这两种情况:
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.
产地:
Error logging capability: (0x01) Error logging supported. General Purpose Logging supported.
Total time to complete Offline data collection: ( 139) seconds.
SCT capabilities: (0x1081) SCT Status supported.
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.
Conveyance self-test routine recommended polling time: ( 3) minutes.
Self-test execution status: ( 0) The previous self-test routine completed without error or no self-test has ever been run.
Extended self-test routine recommended polling time: ( 100) minutes.
Offline data collection status: (0x00) Offline data collection activity was never started. Auto Offline Data Collection: Disabled.
Short self-test routine recommended polling time: ( 2) minutes.
答案 2 :(得分:0)
这些数据的格式并不是最大的,但至少它是可预测的。我们可以根据每行的开头来解析它。
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.
输出:
$VAR1 = {
'Error logging capability' => '(0x01) Error logging supported. General Purpose Logging supported.',
'Total time to complete Offline data collection' => '( 139) seconds.',
'SCT capabilities' => '(0x1081) SCT Status supported.',
'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.',
'Conveyance self-test routine recommended polling time' => '( 3) minutes.',
'Self-test execution status' => '( 0) The previous self-test routine completed without error or no self-test has ever been run.',
'Extended self-test routine recommended polling time' => '( 100) minutes.',
'Offline data collection status' => '(0x00) Offline data collection activity was never started. Auto Offline Data Collection: Disabled.',
'Short self-test routine recommended polling time' => '( 2) minutes.'
};