in reply to regex (between two words)

You can use 2 regexes with the /c modifier to keep the position.

The first can match the start between AAA and the first CCC.

Then you loop over the remaining and push the join with the last match into an array.

Maybe easier to maintain than a complicated regex.

Update

See also

perlretut#Using-regular-expressions-in-Perl

Cheers Rolf
(addicted to the Perl Programming Language :)
Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

Replies are listed 'Best First'.
Re^2: regex (between two words)
by LanX (Saint) on Feb 09, 2020 at 22:01 UTC
    hmm was more complicated than I thought (at least in the debugger)

    DB<173> p $_ = join " ", map { "$_" x 3 } qw/. a b C a d C e C ./ ... aaa bbb CCC aaa ddd CCC eee CCC ... DB<174> ; /(aaa.*?CCC)/gc; $last= $1 # $1 reset +in next debugger line DB<175> @res = map { $last .= $_ } ("", /\G(.*?CCC)/g) DB<176> x @res 0 'aaa bbb CCC' 1 'aaa bbb CCC aaa ddd CCC' 2 'aaa bbb CCC aaa ddd CCC eee CCC' DB<177>

    I think I'd rather prefer splitting the longest match ...

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

      > I think I'd rather prefer splitting the longest match ...

      DB<205> p $_ = join " ", map { "$_" x 3 } qw/. a b C a d C e C ./ ... aaa bbb CCC aaa ddd CCC eee CCC ... DB<206> p ($long) = /(aaa.*CCC)/ aaa bbb CCC aaa ddd CCC eee CCC DB<207> $last = "" DB<208> x map {$last .= $_ } split /(?<=CCC)/, $long 0 'aaa bbb CCC' 1 'aaa bbb CCC aaa ddd CCC' 2 'aaa bbb CCC aaa ddd CCC eee CCC' DB<209>

      Cheers Rolf
      (addicted to the Perl Programming Language :)
      Wikisyntax for the Monastery FootballPerl is like chess, only without the dice