How Do I Replace Text with Number using RegEx?

Hi,

I have a text and I would like to do the following replacements:
januari -> 01
februari -> 02

Is it possible to do this with 1 regex find replace block?

This is what I came up with so far but that doesn't work:

A post was split to a new topic: Replace Month Name with Number

@nrgiser, I hope you don't mind that I have revised your topic title to better reflect the question you have asked.

FROM:
Use regular expressions to replace multiple groups with multiple texts

TO:
How Do I get the Month Number for the Month Name?

This will greatly help you attract more experienced users to help solve your problem, and will help future readers find your question, and the solution.

Hi JmichaelTX,

There are many solutions for this. Javascript is one of them. I currently have a simple Switch block to replace the month name to a number. But to solve it more elegantly, I'm trying to do it with 1 regex expression.

Hi JmichaelTX,

Please leave the title since I'm looking for a regex solution. The month name to month number is just an example. It could also be
potatoes -> 1
vegetables -> 2
...

My apologies for revising the Topic Title with incorrect text. Your OP seemed to indicate you were trying to replace month name, but now with you last post I see you are simply look to replace any arbitrary text with a number.

So, I have revised the Topic Title again. Feel free to change if this does not accurately reflect your request.

Now that you have clarified your objective, I can provide a better solution. I have moved my prior post to a new topic. I'll post back soon with another solution.

Meanwhile, Please read:
Tip: How Do I Get The Best Answer in the Shortest Time?

A post was split to a new topic: [MACRO: Replace Text with Numbers (or, Lookup Data Using RegEx) Example)

Thi is a place where a dictionary can work well. Set up the dictionary mapping, and then do a search and replace, and the replacement value can read from the dictionary.

Keyboard Maestro Actions.kmactions (2.0 KB)

I have just moved the solution/macro I posted earlier to a new topic. You can find it here:

MACRO: Replace Text with Numbers; OR, Lookup Data Using RegEx [Example]

1 Like