in reply to Re: substitution in regular expression
in thread substitution in regular expression

Thanks for the help, it's ok to modify the string. I would like to do it in the way I described if it's possible.
  • Comment on Re^2: substitution in regular expression

Replies are listed 'Best First'.
Re^3: substitution in regular expression
by AnomalousMonk (Archbishop) on Apr 23, 2014 at 20:26 UTC

    Maybe (?) something like this is what you want?

    c:\@Work\Perl\monks>perl -wMstrict -le "my $s = 'ABCDEF'; ;; my @triplets; $s =~ s{ (?= (...)) . }{ push @triplets, $1; ''; }xmsge; ;; print qq{'$s'}; printf qq{'$_' } for @triplets; " 'EF' 'ABC' 'BCD' 'CDE' 'DEF'

    Update: Here's another  s/// solution. I'm not sure I like it so much: I'm always suspicious, perhaps without cause, of code embedded in a regex. In addition, the  @triplets array must be a package global (ideally local-ized) due to a bug in lexical management that wasn't fixed until Perl version 5.16 or 5.18 (I think — I don't have access to these versions and I'm too lazy to check the on-line docs).

    c:\@Work\Perl\monks>perl -wMstrict -le "my $s = 'ABCDEF'; ;; local our @triplets; $s =~ s{ (?= (...) (?{ push @triplets, $^N })) . }''xmsg; ;; print qq{'$s'}; printf qq{'$_' } for @triplets; " 'EF' 'ABC' 'BCD' 'CDE' 'DEF'
Re^3: substitution in regular expression
by AnomalousMonk (Archbishop) on Apr 23, 2014 at 20:09 UTC

    So how should the string end up, as 'DEF' or as 'EF'?

      Well it doesn't matter as long as the dictionary contains DEF in the last entry.
        Well it doesn't matter as long as the dictionary contains DEF in the last entry.

        That appears to conflict with your originally stated requirement that the first of the three letters matched is to be removed.

        Should the string be modified, shouldn't it be modified, or does it not matter whether the string is modified or not?