Parse Number Range in Variable

I have a Regular Expression that will parse the clipboard and grab a capture group such as the numeric range "7-15" but I need a way to tell the macro "for each number between 7 and 15 (inclusive) paste the following...."

For example, if the string is "Read Chapters 7-15" then the macro captures 7-15 correctly, but it needs to output something like this:

Read Chapter 7
Read Chapter 8
Read Chapter 9
Read Chapter 10

The part I'm having trouble with is parsing the 7-15 bit. Do I set a separate variable for each integer? If so, how? I'm looking at the manual and can't figure it out. I'm looking at this and thought I was onto something with this solution but can't figure out the mechanics. Suggestions?

Hello @Steve_Solari,

This will work:


Wow, it's going to take me a minute to figure out how that works but I'm going to study it. Thank you so much!

It first uses RegEx to get the starting chapter number (save it to Var1) and the ending chapter number (save it to Var2).

Then begins the loop:
Beginning with Var1, append "Read Chapter " + "Local__chapterNo" + linebreak ("\n") to the variable "Local__output".
Increase "Local__chapterNo" by 1.

Until "Local__chapterNo" reaches to "Var2 + 1", the loop stops.

Let Local__chapterNo = Var1. This is not necessary. I can just use Var1 instead. However, by introducing "Local__chapterNo", the code looks clearer.
If I not introducing "Local__chapterNo", the quitting loop condition should be "Var2 +1 - Var1" == 0.

1 Like

Yes. This is a fairly easy RegEx, but I have designed one to be specific to "Read Chapers" but then to allow flexibility in the separation of the chapter numbers:
(?i)Read Chapters\h+(\d+)[\h\-]+(\d+)

Just so you know, it could be as simple as:

This just looks for the first two numbers in the string.

I find it best to develop and test RegEx patterns using a tool like

You use a Search using Regular Expression action to extract the start and end chapter number using RegEx Capture Groups.


Then you can use those two KM Variables in a For Each action, which is just a type of repeat loop you see in many languages.


Here's a complete example macro to help get you started.

Below is just an example written in response to your request. You will need to use as an example and/or change to meet your workflow automation needs.

Please let us know if it meets your needs.


Example Output


MACRO:   Looping Through a Range of Numbers [Example]

-~~~ VER: 1.0    2020-09-28 ~~~
Requires: KM 8.2.4+   macOS 10.11 (El Capitan)+
(Macro was written & tested using KM 9.0+ on macOS 10.14.5 (Mojave))

DOWNLOAD Macro File:

Looping Through a Range of Numbers [Example].kmmacros
Note: This Macro was uploaded in a DISABLED state. You must enable before it can be triggered.


Macro Author: @JMichaelTX

This is just an example written in response to the below KM Forum Topic. You will need to use as an example and/or change to meet your workflow automation needs.


  • Carefully review the Release Notes and the Macro Actions
    • Make sure you understand what the Macro will do.
    • You are responsible for running the Macro, not me.

Make These Changes to this Macro

  1. Assign a Trigger to this macro.
  2. Move this macro to a Macro Group that is only Active when you need this Macro.
  3. ENABLE this Macro, and the Macro Group it is in.
    (all shown in the magenta color)
    • Set Source String
      • The text you start with,to be processed
    • Set Variable “Local__Root”
      • The text used in the output for each line. "Read Chapter " in this case.


  1. KM 9.0+ (may work in KM 8.2+ in some cases)
  2. macOS 10.11.6 (El Capitan)+

TAGS: @Regex @Loop @ForEa


Thanks, this should work too. Appreciate the time. Also borrowed your "use at your own risk" comment action from another thread when I shared my final result. Hey by the way, can you take a look at this other one you helped me with a while back? My last comment is at the bottom of the thread. There's something about the week number that's off by a factor of 1.

Didn't know KM has this function. Thanks for sharing!

1 Like