Continuing the discussion from Keyboard Maestro Regular Expression Bug? Trim Whitespace from Start and End of a String:
- Replace Only the First Match in a Complicated String
- There are many times where we do NOT want to replace ALL of a given text (or Regex pattern) in a string.
- For example, the header portion of a long text document (string) where I just want to replace some key terms in the header, like "Author", but NOT in the body of the document.
- Make sure the string end with only one LF, whether the source string has none, or many LF at the end.
- Anyone who has ever used BBEdit to clean up a document will understand this.
- While we could use tools outside of KM to do this, the point of KM is to make it easier for everyone, including those who don't script, and don't have other tools.
- I want to use KM to automate the routine cleanup of documents/email/downloads that I receive.
@peternlewis, please consider this request in the broadest terms of how it might be used.
Every language/app I know of, that supports RegEx, allows the user to perform a Regex Replace with Global matches either on or off.
KM does NOT provide this, and thus produces unexpected results for this simple use case:
- Ensure that String Ends with One, and Only One LF
In this example, I will use a bullet • in the results to make it easier to read.
Reference Case: Regex101.com
-~~~ VER: 1.0 2021-06-18 ~~~
Requires: KM 8.2.4+ macOS 10.11 (El Capitan)+
(Macro was written & tested using KM 9.0+ on macOS 10.14.5 (Mojave))
Make sure text ends with One an Only One LF.kmmacros
Note: This Macro was uploaded in a DISABLED state. You must enable before it can be triggered.