#!/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) captures/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 Order 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."; # Multiple 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 regex 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"; }