http://www.perlmonks.org?node_id=724923


in reply to Regex exact pattern match problem!

This regex should do what you want: ((?:3{4})+)

With the /e modifier you can simplify your program a bit:

#!/usr/str/perl use strict; use warnings; my $toTranslate = "333333333333333333333333333333333333333333333333333333333333333333333 +333333333333333333333333333333333333333333333333233333333322333333333 +233333333333333333333333333333333323333333322333333332333333333"; 1 while $toTranslate =~ s/((?:3{4})+)/"[" . length($1) . "]"/ge; print $toTranslate,"\n";

Replies are listed 'Best First'.
Re^2: Regex exact pattern match problem!
by AnomalousMonk (Archbishop) on Nov 20, 2008 at 20:10 UTC
    1 while $toTranslate =~ s/((?:3{4})+)/"[" . length($1) . "]"/ge;
    I don't understand the point of the 1 while in the expression above. The substitution seems to work just fine without it.
    >perl -wMstrict -le "for my $string ('3' x 33332, @ARGV) { $string =~ s{ ( (?:3{4})+ ) }{ '[' . length($1) . ']' }xmsge; print $string; } " 3333333333223333322 3333333333333333333333333333333333333333333333333333333333333333333333 +333333333333333333333333333333333333333333333332333333333223333333332 +33333333333333333333333333333333323333333322333333332333333333 [33332] [8]3322[4]322 [116]32[8]322[8]32[32]32[8]22[8]2[8]3