Stripping non-final punctuation from a string

I’m trying to create a macro which will rename files as they populate a given folder by removing any repeating punctuation. I just can’t wrap my brain around using Regex…

Here are a few examples:

Before:
This is a string....!
This is another...!!
What is this..!?!?

After:
This is a string!
This is another!
What is this?

This is probably easy to do, but what do you want the macro to do if there is already a file in use with the same name? Anyone who writes a solution will want to know what you want it to do in this case.

By the way, punctuation marks like the ones you use are a bad idea in a UNIX operating system. It can be very hard for command line commands to specify those files.

1 Like

I take it that you want to remove all punctuation marks other than the final one. If that is the case, I offer this solution:

(.*?)(?:[[:punct:]]*)([[:punct:]])

I have assumed that punctuation marks will only occur at the end of the name.

The final capturing group there matches the final punctuation mark. Before it is a non-capturing group that matches any preceding punctuation marks. The first capturing group matches anything up to that point.

Note: the example is only intended to illustrate the text replacement and not anything to do with a file renaming macro.

1 Like

It is a point worth bearing in mind, but this not necessarily a problem since one could wrap the name in quotes or escape the problem characters.

Thank you so very much for your help. I’m so appreciative! :grinning:

1 Like