Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

regex (between two words)

by nickwest (Initiate)
on Feb 09, 2020 at 15:38 UTC ( #11112687=perlquestion: print w/replies, xml ) Need Help??

nickwest has asked for the wisdom of the Perl Monks concerning the following question:

Hello!
$_ = 'aaa bbb ccc ddd ccc eee'; (@qqq) = /(aaa.*?ccc)/g; print join("/n", @qqq);
result:
aaa bbb ccc

But I need one more output - 'aaa bbb ccc ddd ccc'
The string may consist more 'ccc' words.

Replies are listed 'Best First'.
Re: regex (between two words)
by tybalt89 (Parson) on Feb 09, 2020 at 15:54 UTC
    #!/usr/bin/perl use strict; # https://perlmonks.org/?node_id=11112687 use warnings; $_ = 'aaa bbb ccc ddd ccc eee'; my @qqq; /(aaa.*?ccc)(?{push @qqq, $&})(*F)/g; print join("\n", @qqq, '');

    Outputs

    aaa bbb ccc aaa bbb ccc ddd ccc
      Great! Thank you very much!
      If there is more than one aaa this approach would try to match all possible combination intervals, right?

      Not sure if the OP only wants to start from the first aaa

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

        All possible combinations, yes.

        This solution *is* correct because it passes all the provided test cases. (hehehe)

Re: regex (between two words)
by LanX (Archbishop) on Feb 09, 2020 at 19:37 UTC
    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

      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

Re: regex (between two words)
by gurung (Sexton) on Feb 09, 2020 at 18:20 UTC
    regex using lookbehind
    $_ = 'aaa bbb ccc ddd ccc eee'; (@qqq) = /(aaa.*(?<=ccc))/g; print join("/n", @qqq);
      my bad. I didn't see you wanting list of matches.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://11112687]
Front-paged by haukex
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (5)
As of 2020-02-19 23:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What numbers are you going to focus on primarily in 2020?










    Results (86 votes). Check out past polls.

    Notices?