Remove last character from filename only if it matches...

A swapping-ground for Regular Expression syntax

Remove last character from filename only if it matches...

Postby Punt1971 » Mon Mar 01, 2021 12:33 pm

Hi, need a little help!

I'm trying to remove the last character of a filename but only if it matches a hyphon, eg...

Code: Select all
filename.jpeg
filename-.png   --> filename.png
file name.doc
file name 3.mp4
file name -.dxg --> file name .dxg
file.name.mov
file-name-.mov  --> file-name.mov
file-name.mov

Note: The filename may contain other hyphons and other valid non alphanumeric characters.

Hope that makes sense??

Cheers!
Punt1971
 
Posts: 3
Joined: Mon Mar 01, 2021 12:24 pm

Re: Remove last character from filename only if it matches...

Postby Luuk » Mon Mar 01, 2021 1:31 pm

The easy way is putting a checkmark in "v2", so the "Match" can be like -$
Luuk
 
Posts: 320
Joined: Fri Feb 21, 2020 10:58 pm

Re: Remove last character from filename only if it matches...

Postby Punt1971 » Mon Mar 01, 2021 4:38 pm

Luuk wrote:The easy way is putting a checkmark in "v2", so the "Match" can be like -$


Sorry, I should have mentioned, I'm scripting this using the command line tool.
Punt1971
 
Posts: 3
Joined: Mon Mar 01, 2021 12:24 pm

Re: Remove last character from filename only if it matches...

Postby Luuk » Mon Mar 01, 2021 5:39 pm

With command line, the format is like /regexp:(.+)-$:\1
Luuk
 
Posts: 320
Joined: Fri Feb 21, 2020 10:58 pm

Re: Remove last character from filename only if it matches...

Postby Punt1971 » Mon Mar 01, 2021 7:05 pm

Yeah, I know the format, been using it for many years just can't figure out this particular regex.

(.+)-$ did the trick

Cheers! :)
Punt1971
 
Posts: 3
Joined: Mon Mar 01, 2021 12:24 pm


Return to Regular Expressions