Shorten Best Buy URL

Hello friends - let's say I copy a Best Buy URL, for example:

https://www.bestbuy.com/site/apple-airpods-pro-2nd-generation-with-magsafe-case-usbc-white/6447382.p?skuId=6447382

Is there a way to delete everything between "site/" and "6447382.p?skuId=6447382", for example "apple-airpods-pro-2nd-generation-with-magsafe-case-usbc-white/"

...and delete everything after ".p", for example "?skuId=6447382"

...and then replace the clipboard with a plain (not styled) and shortened URL like this:

https://www.bestbuy.com/site/6447382.p

Thanks in advance!

Using the same method as shown in your question about the Home Depot URL, use the Regex Search and Replace with these values:

Search: (.*\/site\/).*?\/(.*?p).*$
Replace: $1$2

I didn't test in Keyboard Maestro, but in my regex tester, that does what you want.

-rob.

Thanks as always Rob. I'm not sure what's wrong, I've been unable to create this macro, and my Home Depot macro stopped working. Dr. Drang also made me a shorten Amazon URL macro that has stopped working as well.

Should I post my setup and steps to reproduce in a new thread?

Thanks again.

Yea, I'd say post them both in one thread, if neither are working. The crowd can then take a look at the code and help figure out what's going on.

BTW, I also have a macro that "decrufts" many URLs, including both Home Depot and Best Buy:

It's probably overly powerful for your needs, but I wrote it to get rid of all the cruft in a lot of URLs I get on email lists.

-rob.

Thanks so much Rob.

It is working, it's just taking a few seconds longer than usual for some reason. Your RegEx works perfectly.

Thanks again!

Using Variable Arrays work as well.

image

2 Likes

@kcwhat - Clever approach.

I wondered why you used [-1] instead of [2], but it seems like it's a quick way to identify the last delimited value in an array without having to know how many values there are. Is that why?

Short Answer: Sure. It was easy to extract what the OP didn't need without using Regex (in this case).

Long Answer: It's a copy cat league and I am a student of @ComplexPoint. He taught me how to use arrays and I wanted him to be semi-proud of me for knowing the basics. Plus, @griffman had the OP covered with the regex version.

1 Like

Yes -- array elements can be accessed "first to last" with [1], [2], etc but you can also go "last to first" using [-1], [-2]...