Some variations, just for fun...
#!/usr/bin/perl
use strict;
use warnings;
use 5.012;
# 901974
say ">\t Iteration 1";
my $text = "some regex here with lots of captures";
say "\$text: $text";
if ( my @matches = $text =~ /some (regex) here (with) lots (of) captur
+es/i ) {
say join "\t", @matches;
}
say "\n>\t Next, testing \$text1 in Iteration 1ALT";
my $text1 = "George's regex is an example of writing sample text with
+an excess of complications";
# NB O
+rder retained
say "\$text1: $text1";
if ( my @matchesALT = $text1 =~ /.*?(regex).*?(with).*?(of).*?/i ) {
say join "\t", @matchesALT;
} else {
say "\t No matchesALT";
}
say "\n>\t Next, testing \$text2 in Iteration 1ALT";
my $text2 = "regex captures with many different matches are a horse of
+ another color than a regex with a single match.";
# Multipl
+e cases for 2 matches
say $text2;
if ( my @matches2 = $text2 =~ /.*?(regex).*?(with).*?(of).*?/ig ) {
say "Iteration 2";
say join "\t", @matches2;
say "\n>\t Next, 2a with the same \$text2";
}
if ( my @matches2a = $text2 =~ /(?:regex)|(?:with)|(?:of)/ig ) {
say join "\t", @matches2a;
say "\n>\t Next, testing \$TextALT in Iteration 2ALT";
}
my $textALT = "The result of the example when text is written with reg
+ex order changed is different";
say "\$textALT: $textALT";
if ( my @matchesALT = $textALT =~ /.*?(Regex).*?(with).*?(of).*?/ig )
+{
say join "\t", @matchesALT;
} else {
say "Iteration 2ALT had no matches (because the order failed)"
+;
}
say "\n>\t Iteration 3 is next";
my $text3 = "regex captures with many different matches are a horse of
+ another color than a regex with a single match.";
say "\$text3: $text3";
if ( my @matches3 = $text3 =~ /(Regex) captures (with) many different
+matches are a horse (of) another color than a (regex) (with) a single
+ match\./ig ) {
say "Iteration 3";
say join "\t", @matches3;
} else {
say "No matches in iteration 3";
}
OUTPUT
> Iteration 1
$text: some regex here with lots of captures
regex with of
> Next, testing $text1 in Iteration 1ALT
$text1: George's regex is an example of writing sample text with an ex
+cess of complications
regex with of
> Next, testing $text2 in Iteration 1ALT
regex captures with many different matches are a horse of another colo
+r than a regex with a single match.
Iteration 2
regex with of
> Next, 2a with the same $text2
regex with of regex with
> Next, testing $TextALT in Iteration 2ALT
$textALT: The result of the example when text is written with regex or
+der changed is different
Iteration 2ALT had no matches (because the order failed)
> Iteration 3 is next
$text3: regex captures with many different matches are a horse of anot
+her color than a regex with a single match.
Iteration 3
regex with of regex with
|