How can I grab a group of numbers at the end of a string in Perl?

I am trying to capture the last digits in this line in a regex group:

Input:

9 Power_On_Hours 0x0032 099 099 000 Old_age Always - 54654

My template:

/Power_On_Hours.+Always\s.+([0-9]{1,5})/

I just can't get it to capture "54654", it returns undef :(

+3
source share
7 answers

Actually, this capture group should capture "4", not undef. Your last .+will eat everything up to the last digit, and then fix it up $1. This revision captures all numbers up to $1:

/Power_On_Hours.+Always\s.+?(\d{1,5})/

? .+ , , (\d).

+9

friedo, , .+ ( ), , .+?.

, , $ , :

/Power_On_Hours.+Always.+?(\d+)$/
+4
#!/usr/bin/perl

use strict; use warnings;

my $s = q{9 Power_On_Hours          0x0032   099   099   000    Old_age   Always       -       54654};

my ($interesting) = $s =~ /([0-9]{1,5})\z/;
print "$interesting\n";
+1

,

$string= q(9 Power_On_Hours          0x0032   099   099   000    Old_age   Always       -       54654);
@s = split /\s+/,$string;
print $s[-1]."\n";

/.+Power.+Always.+[^\d](\d+)$/;
+1

use strict;
use warnings;

my $string= q(9 Power_On_Hours          0x0032   099   099   000    Old_age   Always       -       54654);
my @array = split /\s+/,$string;

print "$array[$#array]\n";
+1

, ?

my ($digits) = $input =~ /(\d+)$/;

A character $binds a group to one or more digits,, (\d+)to the end of a line.

0
source
(?:Power_On_Hours.+Always\D*)(\d+)

It worked for me

http://regex101.com/r/oS8sO5

0
source

Source: https://habr.com/ru/post/1733258/


All Articles