Print all matched parts by regex

Contents

Output

1=apple
2=orange


Explanation

Here are regex flags which I used for this example.
e=execute program in regex in the case of Perl
g=match all, not only once
i=ignore whether character is large or small
s=ignore new line except for Ruby
m=ignore new line in the case of ruby


JavaScript

const value = `This is target 1.
Name is apple.
Target 2 is here. 
Name is orange.`
if (matches = [...value.matchAll(/target\s([\d+]).*?Name\s+is\s+([^\.]+)\./sig)] ) {
    for (match of matches) {
        console.log(match[1]+"="+match[2]);
    }
}

Perl

my $check_value = $value = "This is target 1.
Name is apple.
Target 2 is here. 
Name is orange.";
$check_value=~ s{target\s([\d+]).*?Name\s+is\s+([^\.]+)\.}{
    print $1.'='.$2."\n";
}gesix;

PHP

<?php
$value = "This is target 1.
Name is apple.
Target 2 is here. 
Name is orange.";
$matches = [];
preg_match_all('/target\s([\d+]).*?Name\s+is\s+([^\.]+)\./is', $value, $matches);
if(isset($matches[1])) {
    $i = 0;
    foreach($matches[1] as $match) {
        print($match."=".$matches[2][$i]."\n");
        $i++;
    }
}

Python

import re
value = """This is target 1.
Name is apple.
Target 2 is here. 
Name is orange."""
matches = re.findall( r'target\s([\d+]).*?Name\s+is\s+([^\.]+)\.', value, re.IGNORECASE | re.DOTALL)
for match in matches:
  print(match[0]+"="+match[1])

Ruby

value = "This is target 1.
Name is apple.
Target 2 is here. 
Name is orange.";
matches = value.scan(/target\s([\d+]).*?Name\s+is\s+([^\.]+)\./im)
for match in matches do
  puts(match[0]+"="+match[1])
end