I am trying to grab the values between two delimiters in Perl using regex. I am opening a file and using chomp to go through the file line by line. Example of how the file looks:
"This is <tag> an </tag> example
of the <tag> file </tag> that I
am <tag> trying </tag> to <tag> parse </tag>"
I am able to get the first couple of words: "an", "file", but on the third line I can only get "trying" and not "parse". This is the code I am trying to use:
while (chomp($line = <$filename>)){
($tag) = $line =~ m/<tag>(.*?)<\/tag>/;
push(@tagarray, $tag);
}
I suspect this has something to do with chomp
but don't see how to parse the file differently.
I suspect this has something to do with chomp
No. It is because you are capturing only one value and assigning it to a scalar.
Make the regex global (/g
) and store the results in an array.
#!/usr/bin/env perl
use strict;
use warnings;
use v5.10;
my $line = "am <tag> trying </tag> to <tag> parse </tag>";
my @tags;
(@tags) = $line =~ m/<tag>(.*?)<\/tag>/g;
say join ",", @tags;