Resolved Help with Regex to remove numerics from a word

I have the following name:

Roger Wells12345

I want to remove, using regex, the numerics 12345 and only have Roger Wells as the result. There is no space between Wells and 12345

The 12345 could b anything from 1 to 999999
The name could have numbers other than at the end Roger Wells 2nd12345

Thanks
Roger

Regex Search and Replace.

Find: ^(.*?)\d+$
Replace: $1

The find, briefly explained:

item description
^ Find from beginning of line
( Start capture group
.*? Capture any number of characters, non-greedily
) End capture group
\d Any single digit
+ Match one or more of the preceding character, i.e. any number of digits
$ End of line character

The replace is just the capture group, which is referenced by $1.

-rob.

2 Likes

Or, using a simpler regular expression:

\D

(any non-numeric character)

and obtaining the index (searching from the right) of the first non-numeric character,

we can take a slice:

Without numeric suffix.kmmacros (2.2 KB)


const s = kmvar.local_Source;

return s.slice(
    0,
    1 + [...s].findLastIndex(
        c => (/\D/u).test(c)
    )
);
1 Like

Here's a KM action that can do it. You may want to change the source from "Text" to a "Variable."

image

That'll also delete numbers from "inside" the name, which @RogerW doesn't want to happen.

Simplest solution IMO is a combination of yours and @griffman's:

image

1 Like

If you have Haskell installed and want to try another variant, this code fragment can accomplish the task at hand:

import Data.Char

main :: IO ()
main = interact $ takeWhile (not . isDigit)

You should put the path to your runhaskell executable here:

Screenshot 2025-01-11 at 20.28.14

And download this subroutine...

Execute a Haskell Script.kmmacros (3.6 KB)

Before executing this macro:
Download Macro(s): Numeric suffix removed.kmmacros (3.3 KB)

Macro-Image

Macro-Notes
  • Macros are always disabled when imported into the Keyboard Maestro Editor.
    • The user must ensure the macro is enabled.
    • The user must also ensure the macro's parent macro-group is enabled.
System Information
  • macOS 14.7.2
  • Keyboard Maestro v11.0.3

Oops, you are right. I didn't read carefully.

I have got a bash script to do the job.