Beginner advice needed - Searching Text with RegEx

Hi guys,

could you give me some advice on RegEx?

I just upgraded to KM9 and stumbled upon RegEx for the first time (whole new world of possibilities :slight_smile:) After reading a few tutorials and getting confused I decided to ask the ninjas in this group, while I am beginning to learn.

What I would like to do (with RegEx?)
I would love to find a certain text part in larger texts but the simple search and lookback does not do the job.

Example text:

Main Idea #1
Great: Everything (examples...)
Not so great: Only one thing (examples)
Main Idea #2
Great: Everything (examples...)
Not so great: Only one thing (examples)
Main Idea #3
Great: Everything (examples...)
Not so great: Only one thing (examples)

What I would like to filter out
Only the part "Only one thing (examples)" of Main Idea #2.

So I would need a expression that does:

  1. Look for "Not so great: "
  2. Make sure it's the one after the text "Main Idea #2"
  3. Copy everything after "Not so great: " up until the text "Main Idea #3"
  4. Put it into the clipboard

Thanks in advance for your advice. I will look into several other RegEx tutorials in the meantime.

I'm not one of the wizards here. Just an average user.

Regex is fabulous, but it's also a bit complicated. In most cases there are simpler ways to get the job done than with Regex. I'm not sure if your sole goal is to learn about Regex, or get that job done.

Assuming you put your text into the clipboard, here's how I think I would solve your problem. Notice I redefined your problem a bit, in that the first thing I looked for was "Main Idea #2", not by looking for "Not so great: " and then searching backwards again. That's not really sensible in my opinion.


...that deletes everything up to, but not including, the line "Not so great: "

Then I sorta do the same thing, looking for the first occurrence of "Not so great: " and this time instead of deleting the line I delete only the phrase "Not so great: "


Then I delete everything that starts with the next line containing "Main Idea #3".


Once again, if your whole point was to learn about Regex, then I have failed.

I know enough to know that Regex can probably do what you are wanting, but that would make for a very complicated line. And being a programmer I'm inherently lazy and I'm always looking for the easiest way to solve things. This solution to your problem is probably simpler to read and also probably is easy to adapt to similar problems in the future.

Most of the wizards on this site tell me to upload the code, not just screenshots of my solution. Sometimes I do that, but sometimes I don't. In this case I didn't and my excuse will be because I just woke up before writing this and I'm too tired. :yawning_face:

OK, I think I have a good start for you.
You didn't show an example or so state, but I have allowed for the text in each "Great" and "Not so Great" to have multiple lines. It will, of course, work with just one line.

So here's my revision to your source text:

Main Idea #1
Great: Everything (examples...)
Not so great: Only one thing (examples)
Main Idea #2
Great: Everything (examples...)
and add a second line to Great
Not so great: Only one thing in 2nd NSG (examples)
and add a second line to NSG
Main Idea #3
Great: Everything (examples...)
Not so great: Only one thing (examples)

RegEx Details
(?si)Main Idea #2\RGreat:.*?\RNot so great:\h*(.+?)(?:Main Idea|$)

Example Output


MACRO:   Extract Text Between Key Words [Example]

~~~ VER: 1.0    2019-08-21 ~~~
Requires: KM 9+   macOS 10.11 (El Capitan)+
(Macro was written & tested using KM 9.0+ on macOS 10.14.5 (Mojave))

DOWNLOAD Macro File:

Extract Text Between Key Words [Example].kmmacros
Note: This Macro was uploaded in a DISABLED state. You must enable before it can be triggered.




  • Extract Text Between Key Words


  1. First, make sure you have followed instructions in the Macro Setup below.
  2. Replace the text in the magenta "Set Source String" with your text
  3. Trigger this macro.


  • 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


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

TAGS: @RegEx @Strings @KM9


  • Any Action in magenta color is designed to be changed by end-user


  • To facilitate the reading, customizing, and maintenance of this macro,
    key Actions are colored as follows:
  • GREEN -- Key Comments designed to highlight main sections of macro
  • MAGENTA -- Actions designed to be customized by user
  • YELLOW -- Primary Actions (usually the main purpose of the macro)
  • ORANGE -- Actions that permanently destroy Variables or Clipboards,
    OR IF/THEN and PAUSE Actions


  • While I have given this a modest amount of testing, and to the best of my knowledge will do no harm, I cannot guarantee it.
  • If you have any doubts or questions:
    • Ask first
    • Turn on the KM Debugger from the KM Status Menu, and step through the macro, making sure you understand what it is doing with each Action.

Great work. That's not as complex as I thought it would be. In fact it even looks simpler than my approach.

1 Like

You guys are awesome. Thank you very much.

As you guessed correctly @Sleepy I wanted to get the job done, but my monkey mind got hooked by Regex in the meantime and invested a few hours to understand what @JMichaelTX built and testing a few edits :smiley:.

I hope this thread is helpful for other users too. Thanks again.

@WinWinWin, If one of the above posts solves your problem/question as originally stated, please check the "Solved" checkbox (click for details) at the bottom of that post.

Otherwise, please post your remaining questions/issues about this problem.
If you have other questions, please start a new topic.

Just advice on Regex, seeing as you are new at this:
This article on Daring Fireball contains a lot of really good info:

The BBedit manual is especially valuable as a resource.

1 Like

@WinWinWin, my #1 favorite RegEx tool is:

It is great for both developing and testing RegEx, and provides great explanations as well as showing all match results with capture groups.

Be sure to use the "PCRE" flavor at That provides a very close match to what KM uses (which is ICU Regular Expressions )

We also have a good reference in the KM Wiki:
Regular Expressions

If you have new questions about RegEx, or new problems that might use RegEx, please start a new topic.