How to find all the paired patterns in a variable and write them out and a new line character each matching string?


I have a string variable as


and I have a pattern as like:

/<OPTION [^>]*>\D*([^<]+)/g

How can I print all the matched strings inside that variable and writing to console a newline character - \n after every matched string with Perl?

I recommend the use of TreeBuilder to parse HTML. You will gain performance if use XPath or the look_down() function.

By the way:

my @arr = $variable =~ /<OPTION [^>]*>D*([^<]+)/g;
for(0 .. @arr - 1) {
    print $arr[$_] . "\n";