Jump to content

Regex in Replace step


Recommended Posts

I'm building a (fairly simple) workflow that uses the Selection in macOS function to grab a URL and rewrite parts of it.

 

The one bit I'm struggling with is this: 2066528440_Image2021-01-19at3_08_12PM.jpg.daffa762b1031838c71ccb3fd727486e.jpg

This should grab a 24 character alphanumeric string in the URL and replace it with nothing. But it's not working.

 

I confirmed the Regex is correct in a few online testers, and the rest of the workflow is replacing its bits correctly. What am I missing?

 

An exmaple, my test URL is: https://testsite.com/app/customers/60xxxxf635xxxx/event/600xxxx10fa5exxxxx5d0b077

 

The various parts of my workflow replace testsite.com with testsite.apisite.com, changes /app to /v1, changes /customers to /, SHOULD remove the next string, then changes /event to /conversation

 

Everything works right but the step to change that string via regex.

 

Any help is appreciated.

Link to comment
29 minutes ago, mikejandreau said:

This should grab a 24 character alphanumeric string in the URL and replace it with nothing. But it's not working.

 

Not exactly. You’ve got ^...$, so your regex will only match the entire input, not a substring of it. Is that intentional?

 

Link to comment

Input example: https://site-sandbox.app.com/app/customers/60072f1901cf6356xxxxxxx/event/600740cb5daf2a0yyyyyyyyy

Output Example: 

https://site-sandbox.app.com/app/customers/60072f1901cf6356xxxxxxx/event/600740cb5daf2a0yyyyyyyyy

 

Right now I'm doing it with 5 different steps (replacing various parts of the URL), but if there's a better way, I'd love to hear it.  And since it's a URL, is there a way to make the selection focus on the address bar (I was going to use AppleScript with `tell application "System Events" to keystroke "l" using command down` as a simple way to do it).

Link to comment
5 minutes ago, mikejandreau said:

And since it's a URL, is there a way to make the selection focus on the address bar (I was going to use AppleScript with `tell application "System Events" to keystroke "l" using command down` as a simple way to do it).

 

If you only want to select the URL bar, that's probably the best way to do it. Don't know for sure, since you didn't say which browser you're talking about :)

Link to comment
5 minutes ago, mikejandreau said:

The starting URL is site-sandbox.app.com the resulting URL is site-sandbox.api.app.com

 

That doesn't really answer the question. Perhaps I wasn't clear enough. I need you to indicate which parts of the input URL need to be extracted and inserted into the output URL.

 

Can you please be specific about which parts of the input URL are different in different inputs, or provide some more different examples?

Link to comment
1 minute ago, mikejandreau said:

site-sandbox (which would need to change to site-sandbox.api)

 

Yeah, I can see that. I need you to tell me if the subdomain is always "site-sandbox" and if the domain is always "app.com".

 

It's obvious enough that the long, random strings are variable. It's not clear if any other part of the URL ever changes.

Link to comment
4 minutes ago, mikejandreau said:

I won't pretend to understand what any of that does

 

I had to go look up the Unicode properties you were using. That's regular expressions for you.

 

The parentheses indicate "match groups", which you can insert into the replacement text.

 

([^.]+) = match everything that’s not a dot (match group 1)
\. = literal dot
([^/]+) = match everything that’s not a slash (match group 2)
/app/customers/ = exactly what it says
\w+ = one or more “word” characters (a-z, A-Z, 0-9)
/event/ = exactly what it says
(\w+) = one or more “word” characters (match group 3)

 

In the replacement text $1.api.$2/v1/conversations/$3, $1, $2, and $3 are placeholders for the match groups.

 

 

Link to comment
8 minutes ago, mikejandreau said:

And I tell the thing it's a match group because it's in parenthesis, right?

 

Yup. If you want to extract specific parts of the regular expression, surround them in parentheses. Then you get corresponding placeholders you can insert into the replacement text.

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...