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

Is it possible to use repetition within a Regular Expression Substitution? Apologies if this is a simple solution, but I've done some digging and haven't been able to find an answer.
I have a simple logfile, that works with an existing script, however sometimes I receive an improperly formatted file.
What I'm trying to do is a simple commandline perl to replace a line that starts with 40 hyphens "-", and I need to replace it with 80 asterisks.
instead of typing the 80 asterisks is it possible to use the "x" repetition within the substitution?
e.g.
perl -pi -e 's/^\-{40,40}^M*$/((\*) x 80)/' logfile
that is the "\*" x 80 , within the regular expression,..or something to that effect.,
Thanks
  • Comment on Is it possible to use repetition within a Regular Expression Substitution?
  • Download Code

Replies are listed 'Best First'.
Re: Is it possible to use repetition within a Regular Expression Substitution?
by shmem (Chancellor) on Mar 20, 2010 at 01:13 UTC

    Almost... use the 'e' modifier to the regex:

    perl -pi -e 's/^\-{40,40}^M*$/"*" x 80/e' logfile

    whih means that the substitution part is treated as expresion and its result is substituted.

Re: Is it possible to use repetition within a Regular Expression Substitution?
by ikegami (Patriarch) on Mar 20, 2010 at 02:45 UTC

    ^M? is that a literal Carriage Return? It might be more readable to use \r, \015 or \x0D instead.

    And then there's "\-" which can be simplified to just "-".

    Finally, "{40,40}" can be written as "{40}".

    /^-{40}\r*$/
Re: Is it possible to use repetition within a Regular Expression Substitution?
by ww (Archbishop) on Mar 20, 2010 at 01:30 UTC

    Update: Not directly, by any means I've found (nor by any explicitly sanctioned by </update> shmem found a "direct" means... but see Friedl, Mastering Regular Expressions, anyway.

    However: TIMTOWTDI:

    #!/usr/bin/perl use strict; use warnings; # 829735 my $replacement ="*"x80; print "$replacement \n"; my @logfile = <DATA>; for my $logfileline(@logfile) { $logfileline =~ s|^\-+|$replacement|; print "$logfileline \n"; } __DATA__ phoney content -------------------------------------- more dummy stuff

    which appears to do what you want:

    ww@GIG:~/pl_test$ perl 829735.pl ********************************************************************** +********** phoney content ********************************************************************** +********** more dummy stuff ww@GIG:~/pl_test$

    NB: \-+ in the regex because I'm too lazy to count the hyphens. :-)

Re: Is it possible to use repetition within a Regular Expression Substitution?
by JavaFan (Canon) on Mar 20, 2010 at 13:14 UTC
    I would write that as:
    my $stars = "*" x 80; s/^-{40}\cM*$/$stars/;
    Or even
    my $stars = "*" x 80; my $dashes = "-" x 40; s/^$dashes\cM*$/$stars/;
Re: Is it possible to use repetition within a Regular Expression Substitution?
by Anonymous Monk on Mar 20, 2010 at 03:39 UTC
    perl -pi -e 'BEGIN{$x = "*" x 80} s/^-{40}^M*$/$x/' logfile