user1440061
user1440061

Reputation: 445

Perl Search between key words

Okay, so let's say I have the following in a file I'm reading in:

File:foobarbar.xml
abc
def
ghi
File:barbar.xml
jkl
mno

I would like for the output to be:

foobarbar
barbar

But for some reason I'm only getting

foobarbar

I am using the following code:

if (/File([\s\S]+?)xml/) {
    $key= $1; 
    print OUTPUT "$key\n"
}

Note: in the above code, $key is set to $number one, not $lower case letter L. Any help would be greatly appreciated!

Upvotes: 1

Views: 94

Answers (2)

John Corbett
John Corbett

Reputation: 1615

that regex only matches the first file, use the /g modifier to get all matches

while (/File:(.*)\.xml/g) {
    $key = $1;
    print OUTPUT "$key\n";
}

Upvotes: 2

perreal
perreal

Reputation: 98088

Code below works as expected when you read the input line by line:

while(<>) {
  if (/File:([\s\S]+?)\.xml/){
    $key= $1; 
    print "$key\n";
  }
}

Upvotes: 2

Related Questions