How can I take a TripAdvisor Restaurant Review URL like this:
...and shorten it to something like this (i.e. remove everything after the second number):
https://www.tripadvisor.com/Restaurant_Review-g34515-d12538286
How can I take a TripAdvisor Restaurant Review URL like this:
...and shorten it to something like this (i.e. remove everything after the second number):
https://www.tripadvisor.com/Restaurant_Review-g34515-d12538286
Try this approach. It seems to work for me. You can replace the input box with a variable box if you have the text stored in a variable.
sed -E 's/([0-9]+[^0-9]*[0-9]+).*/\1/'
and another approach might just be to split on -Reviews-
TripAdvisor link before "-Reviews-".kmmacros (22 KB)
return kmvar.local_Source
.split("-Reviews-")[0]
For this sort of thing you are looking for patterns you can use. More samples are better for that.
From just the one, the "KM action" version of @ComplexPoint's JavaScript split
would be:
If you think the -
s are a more reliable splitter you could do:
...which looks really verbose, but is nothing more than "take the first three -
delimited items and join them together with a -
.
And there's the ever-fun RegEx version of the same:
Thank you to @Nige_S – I found that very helpful – for some reason I had formed the impression that custom delimiters for Keyboard Maestro variable arrays were restricted to single-character lengths.
@peternlewis Splitting being an intelligible and easy solution to so many problems, I wonder if it might be helpful to show an example of a multi-character delimiter (for slow horses like myself) on the manual:Variables [Keyboard Maestro Wiki] wiki page ?
Thanks for the code. I apologize for being dumb, but I'm having a hard time getting this to work.
The idea would be that it would be different TripAdvisor restaurants URLs (whatever is copied to the clipboard). For example, shorten:
...to:
https://www.tripadvisor.com/Restaurant_Review-g60763-d27114564.html
Or shorten:
...to:
https://www.tripadvisor.com/Restaurant_Review-g60763-d12652491.html
Do you know how I can get your "TripAdvisor link before "-Reviews-".kmmacros" to work for URLs like the ones above?
Thank you!
Can you unpack that to the point where we have a sense of:
?
Hello friends - I'm trying to shorten TripAdvisor Hotel URLs. For example, shorten:
...to:
https://www.tripadvisor.com/Hotel_Review-g49022-d1382115.html
ChatGPT said to make this but I can't get it to work:
Approximate retrievals by Large Language Models are no better than a coin toss in this context
The first action makes no sense since it is using the SystemClipboard token before anything is copied.
Command-L, Copy should get the URL from the web browser into the system clipboard.
After that you need to look at what you want to do. Firstly, the brackets are not balanced. Secondly, [^\/]+
will match one or more non-/ characters, greedily which means it will match everything, but then you want to match / and there is no more / in the URL.
Try something like:
^(.?-.?-.?)-.
That will match from the start of the text, any number of characters (non-greedily) followed by a hyphen, repeated three times and the rest of the string, capturing up until before the last of the three hyphens.
I have moved the duplicate topic back on to this topic.
Did you try my version of the split
method? It is, perhaps, more user-friendly if you aren't into JavaScript or RegEx.
To explain it further...
To get from:
https://www.tripadvisor.com/Restaurant_Review-g60763-d27114564-Reviews-Isla_Co_Midtown-New_York_City_New_York.html
...to:
https://www.tripadvisor.com/Restaurant_Review-g60763-d27114564.html
...you can get everything before -Reviews
, which gives you:
https://www.tripadvisor.com/Restaurant_Review-g60763-d27114564
...then add .html
to the end:
https://www.tripadvisor.com/Restaurant_Review-g60763-d27114564.html
As a macro, based on yours, and taking the long way round so you can see the steps: