maxedmelon
maxedmelon

Reputation:

How do I save matched parts of a regex in Perl?

I want to sort out all APIs in the following text file:

DLL Name: msvcrt.dll
...
DLL Name: msvcrt.dll
...
DLL Name: KERNEL32.dll
...
DLL Name: WSOCK32.DLL

I thought of something like

$infostring = lc($infostring);
while ($infostring =~ /dll name:/g)
{
 print "found dll\n";
}

the only problem is, how to get the actual dll names or at least the position of the found string?

Upvotes: 0

Views: 3773

Answers (2)

Dave Sherohman
Dave Sherohman

Reputation: 46187

You need to extend your regex to capture the name of the DLL:

$infostring = lc($infostring);
while ($infostring =~ /dll name: (\S+\.dll)/g) {
  print "found dll: $1\n";
}

The \S+\.dll will match one or more non-whitespace characters followed by ".dll" and the parentheses will collect the text matched inside them and store it in the variable $1. (If you had more than one set of parentheses, the second would go into $2, the third to $3, etc.)

Edit: Looks like the input specification was changed by an edit to the question while I was writing my answer... The above would be for a single input string containing all DLL names. Under the new format, with each one on a separate line, you'd instead want to use:

while (my $infostring = <$input_filehandle>) {
  $infostring = lc($infostring);
  print "found dll: $1\n" if $infostring =~ /dll name: (\S+\.dll)/;
}

No need to mess with /g on the regex or looping over matches if there won't be multiple matches in a single line.

Upvotes: 9

Matthew Scharley
Matthew Scharley

Reputation: 132254

while ($infostring =~ /DLL Name: (.*)/g)
{
    print "found dll: $1\n";
}

Please read the perlre manual page. You need to use a capturing group (denoted by the parenthesis) to capture the DLL's name. You can then reference the captures later by using $1, $2, ..., $n

Upvotes: 1

Related Questions