I'm trying do write a one line RE to strip sequence numbers off
filenames.  The filename can may have:
No sequence numbers
   or
Start with a variable number of digits,
Followed by an optional character between a and c
Followed by a compulsory - or _.
The characters - and _ can occur in the filename.

In the code below, the first RE strips the "a" off apple but
by testing for digits first, this can be overcome.
Is there a RE which will remove the need for an if test?

The @ar contains all variations of the filename.

*** CODE ***
#!/usr/bin/perl -w
use 5.14.0;
my @ar = ( "1234_apple.mpg",
    "1234-apple.mpg",
    "123a_apple_a.mpg",
    "1234a-apple-b.mpg",
    "apple-a.mpg",
    "apple_a.mpg",
    "apple.mpg"
        );
foreach ( @ar) {
    (my $newname = $_) =~ s/(?:^\d+)*        # match optional digits
                    (?:[a-c])*        # match an optional alpha a to c.
                    (?:[-_]*)?        # match the compulsory - or _
                    (.*)/$1/x;        # match the file name.
    say "$_\t\t$1";
}
# Two step RE.
say "\nTwo step RE\n";
foreach ( @ar ) {
    my $oldname = my $newname = $_;
    if( $newname =~ s/^\d+(.*)/$1/ ) {
        $newname =~ s/(?:[a-c])*(?:[-_])?(.*)/$1/;
    }
    say "$oldname\t\t$newname";
}

--
Peter Gordon, pete...@netspace.net.au on 01/09/2014



--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to