LinuxQuestions.org
Visit Jeremy's Blog.
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, 10:07 AM   #1
mddnix
Member
 
Registered: Mar 2013
Distribution: Redhat, Ubuntu
Posts: 540

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, 10:16 AM   #2
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 1,496

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

Rep: Reputation: 5250Reputation: 5250Reputation: 5250Reputation: 5250Reputation: 5250Reputation: 5250Reputation: 5250Reputation: 5250Reputation: 5250Reputation: 5250Reputation: 5250
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


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 05: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 02:10 AM
Perl to find regex and print following 5 lines after regex casperdaghost Linux - Newbie 3 08-29-2010 09:08 PM
regex with sed to process file, need help on regex dwynter Linux - Newbie 5 08-31-2007 06:10 AM
Need a regex, I suck at regex's d3funct Programming 4 02-25-2002 09:28 PM

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

All times are GMT -5. The time now is 03:59 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