mikejandreau Posted January 19, 2021 Share Posted January 19, 2021 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: 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
deanishe Posted January 19, 2021 Share Posted January 19, 2021 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
mikejandreau Posted January 19, 2021 Author Share Posted January 19, 2021 Aha. That does it. Now I just need to figure out how to only get it to match the first 24 character string and not the second one and I'll be good to go. Thanks for the help! Link to comment
deanishe Posted January 19, 2021 Share Posted January 19, 2021 This looks like it could all be done with a single regex. Do you have some examples of input and expected output? Link to comment
mikejandreau Posted January 19, 2021 Author Share Posted January 19, 2021 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
deanishe Posted January 19, 2021 Share Posted January 19, 2021 4 minutes ago, mikejandreau said: Input example: https://site-sandbox.app.com/app/customers/60072f1901cf6356xxxxxxx/event/600740cb5daf2a0yyyyyyyyy Output Example: https://site-sandbox.app.com/app/customers/60072f1901cf6356xxxxxxx/event/600740cb5daf2a0yyyyyyyyy They're the same… Link to comment
deanishe Posted January 19, 2021 Share Posted January 19, 2021 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
mikejandreau Posted January 19, 2021 Author Share Posted January 19, 2021 (edited) It's been a day, sorry. Input example: https://site-sandbox.app.com/app/customers/60072f1901cf6356xxxxxxx/event/600740cb5daf2a0yyyyyyyyy Output Example: https://site-sandbox.api.app.com/v1/conversations/600740cb5daf2a0yyyyyyyyy Edited January 19, 2021 by mikejandreau Link to comment
deanishe Posted January 19, 2021 Share Posted January 19, 2021 So you basically just want to get the last element of the URL and add it on the end of a different URL? Or does the domain change? Or any other segment or the URL? Link to comment
mikejandreau Posted January 19, 2021 Author Share Posted January 19, 2021 The core URL does change (I just edited my last post to reflect that). The starting URL is site-sandbox.app.com the resulting URL is site-sandbox.api.app.com Link to comment
deanishe Posted January 19, 2021 Share Posted January 19, 2021 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
mikejandreau Posted January 19, 2021 Author Share Posted January 19, 2021 Gotcha. If the input URL is: https://site-sandbox.app.com/app/customers/60072f1901cf6356xxxxxxx/event/600740cb5daf2a0yyyyyyyyy The bits that would need to be part of the output URL would be: site-sandbox (which would need to change to site-sandbox.api) 600740cb5daf2a0yyyyyyyyy (from the end of the input URL) Link to comment
deanishe Posted January 19, 2021 Share Posted January 19, 2021 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
mikejandreau Posted January 19, 2021 Author Share Posted January 19, 2021 Yessir, the site-sandbox bit varies from site to site that this would run against. The domain root domain would not change. Link to comment
deanishe Posted January 19, 2021 Share Posted January 19, 2021 Replace regex ([^.]+)\.([^/]+)/app/customers/\w+/event/(\w+) with $1.api.$2/v1/conversations/$3 mikejandreau 1 Link to comment
mikejandreau Posted January 19, 2021 Author Share Posted January 19, 2021 Amazing. I won't pretend to understand what any of that does, but it works, so thank you! Link to comment
deanishe Posted January 19, 2021 Share Posted January 19, 2021 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
mikejandreau Posted January 19, 2021 Author Share Posted January 19, 2021 Got it. And I tell the thing it's a match group because it's in parenthesis, right? I think I managed to modify it for a different part of the workflow already, too. Thanks again! Link to comment
deanishe Posted January 19, 2021 Share Posted January 19, 2021 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
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now