in reply to Filename change with regular expression

I'm not sure whether you mean that:

my $rd = int( rand(100000)) + 999999; $filename =~ s/_00001\./_$rd./;

Best regards
McA

Replies are listed 'Best First'.
Re^2: Filename change with regular expression
by Anonymous Monk on Aug 09, 2013 at 19:36 UTC
    That just generates a randon numbers with 7 digits( any better way?). I can get whant I need using an "IF" but I am trying to do all in one regular expression.
    #not using strict and -w for this example only my $rd = int( rand(100000)) + 999999; my @files = qw( XYZ12345_X05_20110805_8999999_00001.TXT XYZ12345_X05_2 +0110805_9999999.TXT); #add dash between $3 and $4 to the .txt before processing them::: #pad $2 with 0s #replace the end of the file with a randon number if the filename ends + with _00001 foreach my $f(@files) { if($f=~/(^\w{3})(\d{5,7})(_\w{1})(\w{1,2}_)(\d{8}\_)(\d{7}_\d{5}\.\w +{3})/i) { (my $xrenamed = $f) =~s/(^\w{3})(\d{5,7})(_\w{1})(\w{1,2}_)(\d{8}\ +_)(\d{7}_\d{5}\.\w{3})/$1.sprintf("%07d",$2).$3."-".$4.$5.$rd.".txt"/ +ie; print "\n\n $xrenamed\n\n"; }else{ (my $xrenamed = $f) =~s/(^\w{3})(\d{5,7})(_\w{1})(\w{1,2}_)(\d{8 +}\_)(\d{7}_\.\w{3})/$1.sprintf("%07d",$2).$3."-".$4.$5.$6/ie; print "\n\n $xrenamed\n\n"; } }
    Thanks!
      but I am trying to do all in one regular expression.

      Why? Other than decreasing readability and maintainability of the code, I see no purpose of having it as a single regular expression.

      Jason L. Froebe

      Blog, Tech Blog

        Other than decreasing readability and maintainability of the code, I see no purpose of having it as a single regular expression.

        I definitely agree. It is often much better to have several regexes is a row, for example to just get rid of all exceptions and special cases, before the regex doing the actual work, rather than trying to pack everything into a single complicated regex.

        For example, suppose that you don't want to apply your substitution on file lines that start with XYZ, contain ZYX or XZY or end with YZZ. Trying to put all this in your single s/// substitution is quickly becoming a nighmare, while something like this is very clear:

        while (<$IN>) { next if /^XYZ/ or /ZYX/ or /XZY/ or /YZZ$/; # removes all special + cases where substitution should not occur s/foo/bar/g; # do something with $_ }

        In the case of the OP, this might be something like this:

        foreach $file (@filelist) { next unless file =~ /_00001\.\w+$/; # now only start for looking for a random number to replace _0000 +1 # ... }

      Hi,

      I read you initial question again and again and I don't understand your answer. Probably to clarify here a little more code:

      use strict; use warnings; use 5.010; my @filenames =('XYZ12345_X05_20110805_9999999.TXT', 'XYZ12345_X05_201 +10805_9999999_00001.TXT'); for my $filename (@filenames) { my $rd = int( rand(100000)) + 999999; say "unchanged_filename: $filename"; $filename =~ s/_00001\./_$rd./; say "changed_filename: $filename"; }

      So, what are my assumptions: You do only have one '.' in your filename seperating the extention 'TXT' from the part you called filename. With this assumption there is exactly one "reference" point in the regex and that is the point separating the extention. That means, if and only if a substring of the format _00001 preceeds the point, therefor is at the end of your "filename" I do a substitution of that part with a random number you provided in your example.

      Have I understood your problem totally wrong?

      Best regards
      McA