Remove everything after 2nd dash

A swapping-ground for Regular Expression syntax

Remove everything after 2nd dash

Postby niceperson » Sun Jun 24, 2018 6:13 am

I wish to remove everything after second occurrence of a dash. No change if there is only one dash in the name.

Example: 1
Original: andy swimming - mandy watching - sandy dancing.jpg
to become: andy swimming - mandy watching.jpg

In this file the name part after second dash, including the second dash, is removed.

Example: 2

Original: andy swimming - mandy watching.jpg
remains as : andy swimming - mandy watching.jpg

No change here because there is only one occurrence of dash in this file name. It would be great if some explanation of the code is also available.

I need help how to do it. I have may files to be renamed in this way.

Thank You
niceperson
 
Posts: 1
Joined: Sun Jun 24, 2018 5:53 am

Re: Remove everything after 2nd dash

Postby therube » Fri Jun 29, 2018 2:33 am

Try:

1:RegEx
Code: Select all
Match:  (.*?) - (.*?) - (.*)
Replace:  \1 - \2


Match any character, non-greedy, up to " - "
Do the same again
And then everything else

\1 matches, andy swimming
\2 matches, mandy watching
\3 matches, sandy dancing

\1 & \2 are included, \3 is simply dropped.

The trouble comes when you have something unexpected.
Like the above actually is keying in on "<sp>-<sp>" rather then simply "-".
Given your example, what I have proposed should work (I think).

But if you wanted it to also account for:
> andy swimming - mandy-watching.jpg
> andy swimming - mandy watching- sandy dancing.jpg
The above would fail that.

But...

1:RegEx
Code: Select all
Match:  (.*?)-(.*?)-(.*)
Replace:  \1 - \2


Along with...

5:Remove -> D/S

might work for those cases too.
therube
 
Posts: 1314
Joined: Mon Jan 18, 2016 6:23 pm


Return to Regular Expressions