LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   What's wrong with this regular expression? Doesn't always work (https://www.linuxquestions.org/questions/linux-newbie-8/whats-wrong-with-this-regular-expression-doesnt-always-work-795261/)

gregorian 03-14-2010 12:02 AM

What's wrong with this regular expression? Doesn't always work
 
I'm trying to write a program that extracts the longest suffix from the filename beginning with a number.

Code:

foreach $i (`dir /b *-*`) {
    $new = $i;
    $new =~ s/\D*(\d+.*)/$1/;
    print $new; 
}

This is the output of dir /b

Code:

Inuyasha Final Act  14 - In Pursuit Of Naraku.mp4
InuYasha Final Act 1 - Naraku's Heart.mkv
inuyasha final act 10 - Flowers Drenched In Sadness.mp4
inuyasha final act 11.mp4
Inuyasha Final Act 13.mp4
InuYasha Final Act 2 - Kagura's Wind.mkv
InuYasha Final Act 3 - Meidou Zangetsuha.mkv
Inuyasha Final Act 4.mp4
Inuyasha final act 5.mkv
inuyasha final act 6.mp4
InuYasha Final Act 7 - The Mausoleum of Mount Azusa.mkv
inuyasha final act 8.mkv
inuyasha final act 9 - Sesshomaru In The Underworld.mp4
Inuyasha The Final Act - 18 - The Day Of Days!.mp4
Inuyasha The Final Act - 19 - Kohaku's Shard.mp4
Inuyasha The Final Act - 20 - When The Jewel Is Whole.mp4
Inuyasha-22.rmvb
Inuyasha-21.mp4
Inuyasha The Final Act - 23 - Naraku The Trap Of Light!.mp4
 Inuyasha - The Final Act - 15.mp4
 Inuyasha - The Final Act - 16.mp4
 Inuyasha - The Final Act - 17.mp4

This is the output of the program:
Code:

14 - In Pursuit Of Naraku.mp4
1 - Naraku's Heart.mkv
10 - Flowers Drenched In Sadness.mp4
2 - Kagura's Wind.mkv
3 - Meidou Zangetsuha.mkv
7 - The Mausoleum of Mount Azusa.mkv
9 - Sesshomaru In The Underworld.mp4
18 - The Day Of Days!.mp4
19 - Kohaku's Shard.mp4
20 - When The Jewel Is Whole.mp4
22.rmvb
21.mp4
23 - Naraku The Trap Of Light!.mp4
15.mp4
16.mp4
17.mp4

I don't know why it doesn't work on all the files. Thank you for your time.

GrapefruiTgirl 03-14-2010 12:08 AM

Hi there :)

While at this time, I do not know exactly what the problem is, I suspect I will not be the only one who doesn't know what you expect to be produced by this code. I don't understand what the question is.

Can you show us an example of what you EXPECT to be produced, when the code is working correctly?

Also, is this some Bash 4.0+ code (i.e. specific functionality to Bash 4)?

Thanks!
Sasha

catkin 03-14-2010 12:48 AM

Quote:

Originally Posted by GrapefruiTgirl (Post 3897462)
Also, is this some Bash 4.0+ code (i.e. specific functionality to Bash 4)?

Not bash 4.0+. Man page for foreach suggests it is TCL.

BeacoN 03-14-2010 01:12 AM

regex headaches fixed :-)
 
In the code you pass in you specify the file must have a dash in it...

dir /b *-*

the "*-*" says that-in your ls you showed files that didn't have a dash-these would be skipped.

gregorian 03-14-2010 07:17 AM

That solved it BeacoN. Small things like these are the hardest to notice. I was just modifying my file renaming program which worked for some other set of inputs. In that program all the files had a '-' in them.

The programming language I'm using is Perl. Thank you for your advice Sasha. I'll be sure to articulate myself better in future posts.


All times are GMT -5. The time now is 12:05 PM.