I just upgraded to KM9 and stumbled upon RegEx for the first time (whole new world of possibilities ) 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:
Look for "Not so great: "
Make sure it's the one after the text "Main Idea #2"
Copy everything after "Not so great: " up until the text "Main Idea #3"
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.
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)
**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:
<a class="attachment" href="/uploads/default/original/3X/5/2/524b569b8abdb5df1c4157701bc0c3e5dc0af0ae.kmmacros">Extract Text Between Key Words [Example].kmmacros</a>
**Note: This Macro was uploaded in a DISABLED state. You must enable before it can be triggered.**
---
### ReleaseNotes
Author.@JMichaelTX
**PURPOSE:**
* **Extract Text Between Key Words**
**HOW TO USE**
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.
**MACRO SETUP**
* **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.
.
* **REVIEW/CHANGE THE FOLLOWING MACRO ACTIONS:**
(all shown in the magenta color)
* Set Source String
**REQUIRES:**
1. **KM 9.0+** (may work in KM 8.2+ in some cases)
2. **macOS 10.11.6 (El Capitan)+**
TAGS: @RegEx @Strings @KM9
USER SETTINGS:
* Any Action in _magenta color_ is designed to be changed by end-user
ACTION COLOR CODES
* 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
**==USE AT YOUR OWN RISK==**
* 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.
---
<img src="/uploads/default/original/3X/6/2/62960d39e38c9ac16d9bea0391b28701c88e2789.png" width="649" height="1069">
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 .
I hope this thread is helpful for other users too. Thanks again.