Problem setting variables with regex in Keyboard Maestro

Hi All,

I am new to KM and am trying to do something that seems pretty simple, but I can't get it to work.

I have a text in my clipboard. In the text there is a piece of text between two html heading tags. I want to extract this text and assign it to a variable named heading

<header>This is my header I want to extract</header>

I try to do this with the following action.

But when I do this, nothing happens. It does not set a variable.

When I use something like:

It does set the variable heading to the clipboard. What am I missing here? I hope someone can help me.

Thanks in advance.

Edited: because the example text disappeared.

trying to do something

To clarify what that is, and enable others to test, you will need to provide:

  • example input
  • an example of the output you are aiming for


Yes, i am sorry. The post wasn't clear enough. I hope now it is.

Stepping back a little further, it might be helpful to know more about the source and context – are you fetching a whole page of HTML there ? Are you fetching it from the web ?

The architecture of regular expressions is known to be ill-suited to parsing, and incapable of recursion generally, but there are other tools which are specifically adapted to parsing HTML for you.

The limitations of a regular expression in this kind of context begin to be apparent if you think of an example like:

<header>Arithmetically, 3 > 2 and 2 > 1, but 3 < 4</header>

The basics of what I am trying to accomplish is the following.

I have a document. For example in Bean, or the regular Mac Texteditor.

In this document i have a few different parts. I choose to put it between because it seemed simpeler, but i could choose any other indicator like ** Header ** or 1. Header here. Just something to mark the start (and end) of a new section.

So i have this document.

Here the title of the document Here is the text of the header Here are a few lines of text from the body. Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu. In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo. Nullam dictum felis eu pede mollis pretium. Integer tincidunt.

I would like to copy those elements separately, then go to a website and paste the elements separately in specific boxes. This last part i know already how to do.

So first copy the text between title tag, then text between header tags, then text between body tags. Go to the website and paste title, header, body.

Bonus question: and when i have this working i want to try to create a conditional like: if there is no text in between to titles, skip this part. That is for later concern.

Always worth checking for typos -- in this case, you've header in your text but heading in your pattern.

Also, I'll add my regular shout out for "variable scoping". It's good to "scope" your variables as tightly as possible -- you'll often use the same variable names in different macros, and you don't them trampling over each other unexpectedly. I'd use Local_heading in this case, to restrict availability to just this instance of this macro (unless I wanted to use the value in other macros after this -- but there are usually better ways of doing that).

There's more about variable scoping in the manual.


If the number of parts is fixed, a single delimiter character (like a line break) may be enough.

See, for example:

Thanks. The typo was not in my original try, just in my explanation here. Excuse for the inconvenience.

Your other tip is a good one. Thanks!

Edit: It seems there still was a typo. Thanks. Everybody for helping out.