in reply to Regex and PID issue

This somewhat simpler regex works for me:

#!/usr/bin/env perl use strict; use warnings; use Test::More tests => 1; my $line = 'root 1435 0.0 0.0 147236 7548 ? Ss 08:46 + 0:02 /usr/local/httpd-2.4.16/bin/httpd -k start'; my $processName = 'httpd'; my $owner = 'root'; my ($pid) = $line =~ qr|$owner\s+(\d*)\s+\d.+$processName|; is ($pid, '1435', 'Match');

Replies are listed 'Best First'.
Re^2: Regex and PID issue
by JonesyJones (Novice) on Jun 16, 2016 at 18:42 UTC
    I agree with you and the simplification. The problem I have is with $1. The value in there is missing the last digit.

      So, add a test for that as well:

      #!/usr/bin/env perl use strict; use warnings; use Test::More tests => 2; my $line = 'root 1435 0.0 0.0 147236 7548 ? Ss 08:46 + 0:02 /usr/local/httpd-2.4.16/bin/httpd -k start'; my $processName = 'httpd'; my $owner = 'root'; my ($pid) = $line =~ qr|$owner\s+(\d*)\s+\d.+$processName|; is ($pid, '1435', 'Match'); is ($1, '1435', 'Match');