Trouble Finding and Replacing Text in a Saved Clipboard

I have a saved clipboard with a file path ending in a file name. I would like to remove the name from the from the path but I'm having trouble understanding how to do that in Regex. I'm thinking I can just find .pdf and work back to the first / but I'm not that familiar with regex.

The file name always ends in .pdf and I want to remove the name up to the first /

It would look like this and I want to remove the end(bold text). Any suggestions how I would go about this?

/Volumes/EDITORIAL/Folder_Name/Sub_folder_name_3007738/3007738_Image_name.pdf

Will this RegEx search work for you?
image

Result:
image

This is very useful, but is there a way to reverse it. Selecting the last part instead of the first?
This 3007738_Image_name.pdf

I know this doesn't use regex, but it is very simple:

1 Like

What. Dang. Of course it would be that easy, so great, thank you!

1 Like

Hey David,

So... You want to remove the parent path and keep the file name – yes?

I think this was not quite clear in your OP.

@tiffle's given you the simplest method.

It's not difficult with RegEx though:

^.+/

RegEx tends to be greedy unless set to not greedy, therefore the pattern finds from the beginning of the string through the last available backslash.

-Chris

2 Likes

@tiffle has provided a direct solution for you, but I'd like to offer these suggestions:

  1. It is best to save plain text in a KM Global Variable, rather than in a Named Clipboard.
  2. In addition to the KM Filter action, you can also use the Split Path action to get one or more parts of the path.

Both of these are better than using RegEx for this use case.

2 Likes

I really appreciate you pointing both of these, they will be well used, thank you!

So this is working great but for some reason I cannot get the bold style to apply in the email. Does someone know why this isn't working?

Hey David,

Mail actions don't support styled text.

Neither does AppleScript per se, although there is a way around that using AppleScriptObjC.

-Chris