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

In reply to Re: array of strings that matched a pattern by ww
in thread array of strings that matched a pattern by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.