For reasons I don't know, the results can be quite different depending on the version of perl.

use strict; use warnings; print "Perl version $]\n"; my $str = "1 plus 2 equals 3 but in this example, AX plus BY equals CZ +, DA plus EBCDEF plus FGH equals G and H plus I plus J plus K equals +L and I wonder what one gets from 7 plus Z"; my $word = "plus"; my @results = $str =~ m{ (?= (\S+ \s+ \Q$word\E \s+ \S+) ) }xmsg; print join("\n", @results), "\n";

produces

Perl version 5.008008 1 plus 2 AX plus BY X plus BY DA plus EBCDEF A plus EBCDEF EBCDEF plus FGH BCDEF plus FGH CDEF plus FGH DEF plus FGH EF plus FGH F plus FGH H plus I I plus J J plus K 7 plus Z

or

Perl version 5.010000 1 plus 2 AX plus BY DA plus EBCDEF EBCDEF plus FGH H plus I I plus J J plus K 7 plus Z

In reply to Re^4: Regular Expression rematch by ig
in thread Regular Expression rematch by dlw

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.