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

I wrote following commands in my script: (testing)
my $file = "textfile.020701_00:08"; my $time = substr($file, 9,12); $time =~ s/(\d{6})(\d{1})(\d{2})(\d{1})(\d{2})/"$1$3$5.monday"/; print "TIME: $time"
What i want is the following result: 0207010008.monday on my display Can you specify bugs in this code. I need your help.star7

Replies are listed 'Best First'.
Re: extract date from filename - need help
by Lachesis (Friar) on Aug 12, 2003 at 16:33 UTC
    Your problem is that your regex effectively boils down to /\d{12}/ ignoring the substitution and variable capture, so it will only match a 12 digit number.
    You actually need to match the _ and : characters.
    What you want would be something like.
    my $file = "textfile.020701_00:08"; my $time = substr($file, 9,12); $time =~ s/(\d{6})_(\d{2}):(\d{2})/"$1$2$3.monday"/; print "TIME: $time";
Re: extract date from filename - need help
by Not_a_Number (Prior) on Aug 12, 2003 at 17:34 UTC

    Why complicate matters?

    my $file = "textfile.020701_00:08"; $file =~ s/\D//g; my $time = $file . '.monday'; print $time;

    dave

Re: extract date from filename - need help
by sweetblood (Prior) on Aug 12, 2003 at 16:39 UTC
    Well the "\d" in your expression means a digit so when you say s/(\d{6})(\b{1}) you are saying find 6 digits and assign them to $1 then find 1 digit and assign it to $2. Your string contains 6 digit followed by an "_" so you expression will fail. if you wish to go this way you would want s/^(\d{6})_(\d{2}):(\d{2})/$1$2$3.monday/ This should give you what you want. You'll notice that I added the "^" to the beginning of the expression to anchor my search to the leftmost position.