Looping 'Text to Variable' With RegEx

I fear this is a very basic question but I've been searching through the forums and wiki for over an hour and I'm starting to doubt my own sanity. I'm sure this is simple but I can't get my head around it.

Goal
I want to copy three sets of usernames and passwords from a webpage (that has a fixed layout) and assign each username and pairing to a hotkey so I can enter each into a website with a single key. (eg. Key 1 = first user, Key 2 = second user etc.). That pasting macro is working fine. It's just a combination of pasting %Variable%demoUsername1% etc along with some tabs and enters.

Where I'm Stuck
I can't for the life of me get a loop working. This is where I'm at in terms of the first user:


Get Credentials.kmmacros (3.8 KB)

I've got the first username and password as the variables demoUsername1 and demoPassword1. Great! But how do iterate through the rest so they become demoUsername2, demoPassword2 etc.? My variable userCredentials has all the text I need.

My basic understanding regex is telling me the syntax is right (regex101: build, test, and debug regex ) but the KM looping has defeated me. I'm sure the answer is simple but I've got myself into a deadend.

Any help would be massively appreciated. I think I've read too many forum posts with more complicated requirements that I've confused myself.

Hi, @me_simon. I'm not entirely clear about your data, but I'm taking a guess:

If so, here is one way:

Download: 01)Get Credentials (@_jims).kmmacros (48 KB)

Macro-Image


Macro-Notes
  • Macros are always disabled when imported into the Keyboard Maestro Editor.
    • The user must ensure the macro is enabled.
    • The user must also ensure the macro's parent macro-group is enabled.

System Information
  • macOS 13.3.1 (22E772610a)
  • Keyboard Maestro v10.2


Just a couple notes:

  • Typically you'd want to use local variables in a macro like this. Click here for more information.
  • I inserted some Group actions (in red) that you can obviously disable or delete. I inserted those actions using a macro I shared elsewhere on the forum).
1 Like

Thanks @_jims . Sorry, I was so focused on other elements that I completely forgot to mention that the data was here: regex101: build, test, and debug regex

I now realise I failed to include the desired output example. The goal being I'd have 3 instances of this for users 1-3.

Thanks for looking into this. I'll test your macro out shortly.

My guess of the input format was close enough that the parsing logic did not have to change. But in this version I inserted the actions that you included above. Note that they are initially disabled, but after confirming that the data is parsed correctly you can enable that group.


Download: 01)Get Credentials (@_jims- v2).kmmacros (40 KB)

Macro-Image


Macro-Notes
  • Macros are always disabled when imported into the Keyboard Maestro Editor.
    • The user must ensure the macro is enabled.
    • The user must also ensure the macro's parent macro-group is enabled.

System Information
  • macOS 13.3.1 (22E772610a)
  • Keyboard Maestro v10.2

Feel free to follow-up if you have any questions.

1 Like

@_jims's macro is well suited to your line-by-line input text, and is nice and clear.

For fun, here's a version that uses "For Each" and substrings -- a technique you might find useful in other situations.

Extract Credentials.kmmacros (5.9 KB)

Image

2 Likes

Thanks so much for this @Nige_S and @_jims.

You've given me two great examples to use. And also super helpful in learning by example which is how I learn the best.

This has to be one of the most generous forums on the Internet!

Thanks again!

3 Likes