RegEx to Remove Junk and Split Names

I have a string and I want to remove any digits and anything between [ ] including the [ ] using RegEx.

Possible input variances:

Clarence Gudgell Wells could be any name.

Input String: Clarence Gudgell Wells
Input String: Clarence Gudgell Wells 3372
Input String: Clarence Gudgell Wells [Test Junk]
Input String: Clarence Gudgell Wells 3372 [Test Junk]

Result wanted Clarence Gudgell Wells.

Thanks.
Roger

Keyboard Maestro Version 10.0, MacOS version 11.6

Use RegEx = (\d+)|(\[.*\])

Search and replace Action

Thanks much that worked great.

One more question I hope it's permitted.

I now have the name Clarence Gudgell Wells

I want to extract the last name Wells into a variable and remove it from the input

Input: Clarence Gudgell Wells
Output to a variable: Wells
Output to a variable: Clarence Gudgell

I tried this and it works on regx101 but not in Keyboard Maestro

Roger
Keyboard Maestro Version 10.0, MacOS version 11.6

Don't think that you can do both at same time in KM

Need to perform separate step in KM

  1. Assign Wells to variable using Search Variable Using Regular Expression action
  2. Remove Wells using Search Replace Text action

Hey Roger,

Of course. Ask all the questions you want – just don't be overly shocked if no one answers.  :sunglasses:

This is easy enough with a little RegEx magic.

-Chris


Split Names With RegEx v1.00.kmmacros (1.5 KB)

Macro-Image

Keyboard Maestro Export

1 Like

Another approach, (less magic, simpler steps), for those who are using Keyboard Maestro 10, might be to:

  • split the string into space-separated words
  • filter the words on whether they:
    • start with numbers, or [
    • or end with ]

1 Like

Not sure if you are using Keyboard Maestro 10 yet, but in case you are, and want to test it, here is a draft of the macro (pictured two posts back) which uses a space-separation of the string into words:

Junk words filtered out.kmmacros (5.7 KB)

1 Like

That would work and I am using Keyboard Maestro 10. But if I am ever going to learn Regex I need to try to use it. But some of my grey hair is starting to be missing. :grinning:

There are two regular expressions in there ...

Like most things, they're best used in very small quantities : -)

(Once a regular expression gets just a little longer and more complicated, a full scripting language, or some KM blocks, will be already be more flexible, easier to adjust and debug, and less prone to cost frustration and time).