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 05-20-2020, 09:07 AM   #1
Madhu Desai
Member
 
Registered: Mar 2013
Distribution: Rocky, Fedora, Ubuntu
Posts: 541

Rep: Reputation: 153Reputation: 153
RegEx - Any better than this?


Hello All

I'm using RegEx to rename a lot of files. Following are some samples of the filename.
Code:
$ ls -1N
01 Linux OS Introduction12m 58s.mp4
02 Subject Intro25s.mp4
03 Learning Objectives23s.mp4
04 Final Analysis3m 12s.mp4
I want to replace characters in bold to nothing. So, the regular expression I'm using is this. I'm also using Positive Lookahead Assertion for safety.
Code:
([0-9]{1,2}[sm]{1}) ?([0-9]{1,2}[sm]{1})?(?=\.mp4$)
Code:
$ prename -nv 's/([0-9]{1,2}[sm]{1}) ?([0-9]{1,2}[sm]{1})?(?=\.mp4$)//' *.mp4
01 Linux OS Introduction12m 58s.mp4 -> 01 Linux OS Introduction.mp4
02 Subject Intro25s.mp4 -> 02 Subject Intro.mp4
03 Learning Objectives23s.mp4 -> 03 Learning Objectives.mp4
04 Final Analysis3m 12s.mp4 -> 04 Final Analysis.mp4
Is this the correct way? Or can I optimize/shorten RegEx even better?

Thanks
 
Old 05-20-2020, 09:16 AM   #2
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 3,670

Rep: Reputation: Disabled
Maybe
Code:
(\d\d?[sm] ?){1,2}(?=\.mp4$)
 
1 members found this post helpful.
Old 05-20-2020, 09:39 AM   #3
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,836

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
probably in two steps:
Code:
\d+s\.mp4 -> .mp4
\d+m \.mp4 -> .mp4
# or
(\d+m )?\d+s\.mp4 -> .mp4
 
1 members found this post helpful.
  


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
Confusing issue with Perl regEx - Regex check seems to require variable being set EnderX Programming 1 09-07-2013 04:36 AM
[SOLVED] differences between shell regex and php regex and perl regex and javascript and mysql golden_boy615 Linux - General 2 04-19-2011 01:10 AM
Perl to find regex and print following 5 lines after regex casperdaghost Linux - Newbie 3 08-29-2010 08:08 PM
regex with sed to process file, need help on regex dwynter Linux - Newbie 5 08-31-2007 05:10 AM
Need a regex, I suck at regex's d3funct Programming 4 02-25-2002 08:28 PM

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

All times are GMT -5. The time now is 05:08 AM.

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