Another Regex Question

I am trying to select everything between the first and last space.

William A. Wells = A.
William Arron Wells = Arron
William Arron Hello Wells = Arron Hello

So you want:

A. Wells =
Arron Wells =
Arron Hello Wells =

Is that right?

No the Wells should not be included

Just
A.
Arron
Arron Hello

Oops - I didn’t realise your post showed

before = after
!

Hi @RogerW. Here's one way:

DOWNLOAD Macro File:
Get Middle Name(s).kmmacros (4.9 KB)
Note: This macro was uploaded in a DISABLED state. It must be ENABLED before it can be run. If it does not trigger, the macro group might also need to be ENABLED.

Macro-image

Or

  • .split
  • .slice
  • .join

Between the first and last space.kmmacros (2.1 KB)


Application("Keyboard Maestro Engine")
    .getvariable("someLines")
    .split(/[\n\r]+/ug)
    .map(
        x => x.split(" ")
        .slice(1, -1)
        .join(" ")
    )
    .join("\n");

Result

1 Like

Try ^\S+? (.+?) \S+?$ to eliminate the second Search action.

1 Like

The simplest one I have is this:

Test regex.kmmacros (2.9 KB)

Click to see macro

Starting with

William A. Wells
William Arron Wells
William Arron Hello Wells

it leaves you with

A.
Arron
Arron Hello

and works with however many lines you want.

1 Like

Thanks mrpasin

This did the trick and also showed me the error I had. I had leading and trailing blanks that needed to be removed to make it work.

Roger

2 Likes