Extract link from clipboard

I have a text in my Clipboard that contains web link, how do I extract the link to use it further in a browser?

The Clipboard is:

Currently running servers:
http://localhost:4899/?token=2e7c376075a :: /home/user

I want to save http://localhost:4899/?token=2e7c376075a to a variable or to a Clipboard, how should I do that?

What regex should I use and where should I put it (what action)?

Thank you

1 Like

Use the Search and Replace action on the Clipboard:

updated 2018-04-08 19:11 GMT-0500

  • Fix error in RegEx
  • It will now match the entire source string
  • And replace it with just the URL

Search:
(?s).*(https?:\/\/[^\h]+).*

Replace:
\1

For details, see regex101: build, test, and debug regex

1 Like

Thanks for the response. I didn't mention that the token in the http address is always different, so thanks for the reg ex.

Do I get it right, by means of using the suggested approach the Clipboard is to be replaced with the http address only? If so, I tried what you've suggested but still the whole thing was in the Clipboard.

image

The Display Clipboard returns the old Clipboard.

image

containing:

Currently running servers:
http://localhost:4899/?token=2e7c376075a :: /home/user

What am I doing wrong?

Thanks

oh, may bad, I didn’t choose Regex in the search options.

Now it removes the link from the Clipboard and leaves everything else, how do I save the link to a Clipboard or another variable?

Thanks!

Sorry, my bad. I was focused on extracting the URL, not matching the entire string.

I have updated my above post, which should now do what you asked.

Please let me know if you have any more issues, questions, and/or suggestions.

1 Like

thanks for the update.

now it removes everything and I get ‘1’ in the clipboard.

I think the problem is in the ‘and replace with’ value. Should it be \1 ?

Yes, just as I stated:

Well, this is what I get step by step, what am I doing wrong? Thanks

image

image

image

image

image

Unless something has changed I believe the only regex replace syntax KM supports is dollar signs, so instead of

\1

try:

$1

1 Like

Thanks! That worked.

1 Like

Actually that changed with some version of KM 8.

So this works fine for me, running:
Keyboard Maestro 8.2 (8.2) on macOS 10.12.6

image

But a search of the KM Wiki does NOT show this change.
Perhaps @peternlewis can tell us exactly when the change was made.

IAC, I have been using \<CaptureGroupNumber> for some time.

Another real example:

Oh, awesome. I prefer the backslash method since it’s what I’m used to in BBEdit (plus it’s easier to type, being one key and all) so this is good news. Thanks! I guess @rover233 must have just been using an older version of KM then.

2 Likes

Right, I have 7.3.1

In the future, please state the version of KM that you are using, if not the current version. Most of us will assume the current version.

It will help you get a better, faster answer, and avoid a lot of wasted time. :wink:

Having said that, I highly recommend upgrading to KM 8. Lot's of great new stuff that give us new capability and make writing macros easier.

Thank you for the suggestion. I had no idea I was not using the latest version.

Hey @rover233,

Yet another good reason to let people know what software you're using.

Let's make that really simple to do:

Paste Keyboard Maestro Version and the macOS System Version

-Chris