[Pdx-pm] regexp and semi-greedy match

Tkil tkil at scrye.com
Sun Oct 14 23:32:08 PDT 2007


>>>>> "Keith" == Keith Lofstrom <keithl at kl-ic.com> writes:

Keith> foo-1.raw, foo-2.raw, ...  foo-9.raw, foo-10.raw, foo-11.raw
Keith> ...  foo-99.raw, foo-100.raw, 

So why not just use a greedy match for everything before the integer
count and ".raw"?  So long as you use "one or more" for the integer
count, and the ".raw" literally, it should Just Work (since the evil
hacker can't prevent the presence of /-\d+\.raw/ at the end of the
file names no matter what they specify).

| my $max_digits = 1;
| my $common_base;
| my @files;
| foreach my $filename ( glob '*.raw' )
| {
|     my ( $base, $num ) = ( $filename =~ m!^(.*)-(\d+?)\.raw$!i )
|       or die "$filename: unrecognized filename";
|     if ( ! defined $common_base ) {
|         $common_base = $base;
|     } else {
|         if ( $common_base ne $base ) {
|             die "$filename: new base '$base' " .
|                 "differs from old base '$common_base'"";
|     }
|     push @files, { orig_name => $filename, num => $num };
|     if ( $max_digits < length( $num ) ) { $max_digits = $length $num ; }
| }
| 
| my $format = $common_base . '-%0' . $max_digits . 'd.raw';
| 
| foreach my $href ( @files )
| {
|     my $orig = $href->{orig_name};
|     my $new  = sprintf $format, $href->{num};
|     rename $orig, $new
|       or warn "rename '$orig', '$new': $!";
| }

Hmm?

t.


More information about the Pdx-pm-list mailing list