How Do I Extract Text from "Fields" in a Multiline String?


I am now trying to get the regular expression from the line below, I manage to copy the data I want to clipboard and it looks like this;

First name::
Middle name(s)::
Last name:
Mobile number::

The question always ends with ':' I'm trying to extract the answers to save as KM variables..
The answers are always on a single line after the question.

Thank you for your assistance.

1 Like

Like this?


Extract answers to variables.kmmacros (4.9 KB)




  • The yellow actions are for demonstration purposes (building the clipboard, displaying the result). You may not need them in your work context.
  • Based on your statement “The question always ends with ':'” I am assuming that any answer is not ending with a ‘:’. If this is not true, then use this slightly more complex regex: (?m)(?<=:[\n\r]).+$
  • If you prefer to work with local variables you can prefix any variable with “Local ”, “Local_” or “Local__”. (Except for variables that are intended for further use with another macro.)

Using a dictionary:

Instead of variables you can also save the answers to a dictionary. The macro then could look like this:


Extract answers to dictionary.kmmacros (5.1 KB)

1 Like

I have moved your posts, and all replies, to a new topic because:

  1. The other topic was over a year old
  2. The other topic already had a "solved" post selected
  3. Your requirements are a lot different

In the future, please start a new topic for a new question.

1 Like

I would use an individual Search Using Regular Expression for each chunk you want to match.

Keyboard Maestro Actions.kmactions (1.7 KB)

That way the order can change and the macro will still work.

Noted that if you want to allow for missing fields, then you need to initially set the match variable (as shown above) and also configure the Search Using Regular Expression action to not notify or fail on error.