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

Hello monks, Code:
$string="abc de fgh ijkl"; while($string =~ /([a-z]+ [a-z]+)/g) { print "$1 $1\n"; }
Outputs:
abc de fgh ijkl
I would like it to output:
abc de de fgh fgh ijkl
I have tried doing:
$string =~ /([a-z]+) ([a-z]+)\G(?: [a-z])*?/g
and using lookarounds but I haven't been able to find the right combination of \G, lookarounds and matching to come out with what I want. I am also aware that I could just do:
my @words =split $string; for($i=0;$i<$#words;$i++) { print "$words[$i] $words[$i+1]\n"; }
I trying to stick to regex's. Thanks, Alex

Replies are listed 'Best First'.
Re: Combinatorial regexing
by blokhead (Monsignor) on May 04, 2007 at 18:36 UTC
    Try something like this:
    while ($string =~ / (?=([a-z]+[ ][a-z]+)) [a-z]+ /gx) {
    This says: look ahead to capture the next two words, without advancing pos. Then with that stuff captured, advance pos as far as the first word. However, I don't see much wrong with using @words like you've done in your last example...

    blokhead

Re: Combinatorial regexing
by johngg (Canon) on May 04, 2007 at 22:27 UTC
    This uses a look-ahead but in a slightly different way to blokhead's solution. The regex looks for and captures a letter sequence then uses a look-ahead with a capture to find another sequence.

    use strict; use warnings; my $string = q{abc de fgh ijkl}; my $rxComb = qr {(?x) (?: ([a-z]+) \s+ (?=([a-z]+)) ) }; while ( $string =~ m{$rxComb}g ) { print qq{$1 $2\n}; }

    Here's the output.

    abc de de fgh fgh ijkl

    I hope this is of use.

    Cheers,

    JohnGG

Re: Combinatorial regexing
by shandor (Monk) on May 04, 2007 at 19:15 UTC
    How about this?
    use warnings; use strict; my $string = "abc de fgh ijkl"; my @array = split /\s+/, $string; my $count = 0; while ($count < $#array) { print $"array[$count] $array[$count+1]\n"; $count++; }


    It's not a regexp, but, it displays the output you want.

    UPDATE: when you read the entire OP, you find funny things... like the fact that the OP-er listed this solution. Oh, well, made me not have to do real work.