Removing characters with Regex

I'm trying to remove some characters from a string using search and replace and Regex.
I find the Regex learning curve a bit overwhelming...

For instance:
HFT-COOL (Surf) - Codingweb
HFT-SURFER (Surf) - Codingweb

would deliver COOL and SURFER.
So the 1st four characters and all characters after the first space need to be removed (including the space itself). Any pointers? Thank you!

\-(.+?)[space]

Just change [space] to a space.

Thanks, a step further! So the solution is Group 1. How do i refer to Group 1 in the "replace with" section?
Tested on https://regex101.com...

$1

(Click on the Gear icon of the action you're using for more help.)

Hmmm, that doesn't work.

Lots of ways to skin this cat.
If you want to make sure you get the text after "HFT-", then this will work.
It also allows for multiple spaces or tabs after the key text.

==UPDATED==: 2020-01-04 14:10 GMT-6
Allow for searched text to be single or multiple lines

Search For:
(?m)^HFT-(.+?)\h+.+$

Replace with:
\1

For details, see regex101: build, test, and debug regex

1 Like

Thanks!!!

Just made an update to handle multiple lines.

Even better! Thanks!

1 Like

Ah, Discourse stripped the leading slash. I've edited the original post to restore it.

The way to avoid Discourse changing your text is to enclose in back quotes like:

  `my text`

Found a cool website to help learn Regex. Maybe someone finds it useful in the future https://regexone.com

1 Like

There are several good tutorial sites for RegEx, but I do like the interactive learning feature of this one. Doing is always the best method for learning.

IĀ“m having trouble using it with my own macro.

Bild 09.09.24 um 17.37

I want to delete everything before ": Ins. 5 -" and also including this string of the front window. The string before the ": Ins." is variable and has never the same length.

Your definition of the retained string is not quite clear to me ā€“ you want:

  • H-Delay Mono ?
  • : Ins. 5 - H-Delay Mono ?

In any case, you can experiment with custom delimiters for Keyboard Maestro Variable Arrays

https://wiki.keyboardmaestro.com/manual/Variables#Variable_Arrays


Numbered segments split on custom delimiter.kmmacros (3.2 KB)

1 Like

I guess I didn't make myself clear. Sorry!
I just need the part after "Ins. x"

So just "H-Delay Mono". The part before the colon always changes.

1 Like

Good ā€“ the macro above should do that:

  • splitting on the custom delimiter "-"
  • taking the resulting parts 2 and 3
  • rejoining those two parts with an inline "-"

(a regular expression would just make things harder)

Maybe I'm missing something here, but is this what You're trying to achieve?

image

 
The regular expression is:

Ins\. \d+ - (.*)

I found an easier solution to my problem.
Thanks anyway!

1 Like