find and delete repeating patter " - 00 - "

A swapping-ground for Regular Expression syntax

find and delete repeating patter " - 00 - "

Postby Nell » Wed Jul 02, 2014 5:36 pm

Any help appreciated on this one..

Code: Select all
" - 00 -"


Where the "00" could be any two digit number, I would like to search for this in my filename and delete it. There may be more hyphens and other numbers in the filename, but there will only ever be one instance of SPACE, HYPHEN, SPACE, DIGIT, DIGIT, SPACE, HYPHEN.




And a separate problem...
I would like to remove everything between the first and last hyphen, leaving just one hyphen. So, from this:

Code: Select all
"I want this text - not this - text - only"


to this:

Code: Select all
"I want this text - only"


Thank you in advance!
Nell
 
Posts: 5
Joined: Tue Jun 03, 2014 2:20 am

Match repeating strings, Match 1st/last hyphen

Postby truth » Thu Jul 03, 2014 1:34 am

To remove a duplicate, repeating-occurence of: " - 2Digits -"
(.*?)( - \d\d -)(.*?)\2(.*)
\1\2\3\4
Text1 - 21 - Text2 - 21 - Text3 -> Text1 - 21 - Text2 Text3
Text1 - 21 - Text2 - 22 - Text3 -> Unaffected (digits not identical)

To remove all text inbetween 1st/last hyphen (keeping 1st hyphen)
(.+?-).*-(.*)
\1\2
Use with caution, very filename destructive
1-2-3-4-5-6-7-Any Text-8-9 -> 1-9
truth
 
Posts: 221
Joined: Tue Jun 25, 2013 3:39 am
Location: Earth, OrionArm, MilkyWay


Return to Regular Expressions


cron