LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-14-2010, 12:02 AM   #1
gregorian
Member
 
Registered: Apr 2006
Posts: 509

Rep: Reputation: 34
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.
 
Old 03-14-2010, 12:08 AM   #2
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556
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
 
Old 03-14-2010, 12:48 AM   #3
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by GrapefruiTgirl View Post
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.
 
Old 03-14-2010, 01:12 AM   #4
BeacoN
Member
 
Registered: Nov 2008
Distribution: Linux Mint
Posts: 56

Rep: Reputation: 17
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.
 
1 members found this post helpful.
Old 03-14-2010, 07:17 AM   #5
gregorian
Member
 
Registered: Apr 2006
Posts: 509

Original Poster
Rep: Reputation: 34
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Regular expression doesnt work in an elsif statement? oinker Programming 2 02-04-2010 01:33 PM
Need help with a regular expression abefroman Programming 2 05-28-2008 09:17 PM
Help with regular expression Feyd-Rautha Programming 8 04-21-2008 11:18 AM
Do we have regular expression in C++ ? indian Programming 4 03-06-2006 09:54 AM
what is wrong with this regular expression in procmail recipe? realos Linux - Software 4 10-20-2005 09:35 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 03:57 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration