Regex - Search and Replace All Digits

I have this that replaces all digits in a string but I need it to not delete the digit in 3rd but all of the other digits.

The 3rd will always be a digit followed by 2 alpha. (3rd, 1st, 2nd etc) but I still want it to delete the other numbers. They can be any digit number from 1 to 99999.

The output for this example should 3rd.

Regex - Search and Replace all Digits copy.kmmacros (3.5 KB)


If you want to match any number that isn't an ordinal, use a negative lookahead and end on a word boundary:


"One more more digits that that aren't immediately followed by 'st', 'nd', 'rd' or 'th', and then a word boundary"


I am 5556 and the 1st of May 1999

would transform to

I am  and the 1st of May 

Note the double space in the middle and the trailing space at the end that you might need to deal with.


Delete Numbers that Aren't Ordinals.kmmacros (3.1 KB)


This may not be the best method, but it's working in my tests. Looking forward to what others may suggest!

1 Like

Gauntlet retrieved, @Nige_S. I'd just look for digits and a word boundary to avoid ordinals:


But then I wonder if that's sufficient. Probably for list of names. But I'd be more comfortable with a more comprehensive spec. You might prefer looking for numbers at the end of a line:


And whitespace may or may not have to be dealt with.


Yes -- that's much better! (Note to self: stop overcomplicating things.)

Agreed. We might even want to start with a \b as well, on the off-chance someone's GivenName is "R2D2" :wink: