First, let's create a test bed so we can tell if we got it right!
use strict;
my $correctanswer = "11408";
my $regexp = qr/^([\d]+)*(hullBts_static)/;
while (<DATA>) {
if ( /$regexp/ ) {
if ( $1 eq $correctanswer ) {
print( "Congratulations, the regexp is correct!" );
exit( 1 );
} else {
die( "Error, match was $1" )
}
}
}
die( "Error, never matched" );
__DATA__
11299 pts/4 00:00:00 su
11303 pts/4 00:00:00 bash
11403 tty2 00:00:00 bsc_static
11406 tty2 00:00:00 rtpTrau_static
11408 tty2 00:11:30 hullBts_static
11418 pts/4 00:00:00 tail
12157 pts/5 00:00:00 su
Well if I run this test code I get: Error, never matched at /tmp/hullbts.pl line 17, <DATA> line 7.
Clearly something is wrong. I suggest taking a closer look at the regexp and what you're trying to match.
First you want the beginning of a line. Then as many digits in a row as possible. Then anything until we find "hullBts_static". With this in mind let's have another look at a regexp: my $regexp = qr/
^ # beginning of line
(\d+) # as many digits as we can find
.* # anything at all
hullBts_static # literally, "hullBts_static"
/x;
When substituting this new regexp we get the following output from the test script: Congratulations, the regexp is correct!
Note that the /x at the end of the regular expression merely allows us to spread the expression over several lines. That regular expression looks, more compactly, like /^(\d+).*hullBts_static/. |