Replacing Substrings in Clipboard or Variable

I'm trying to create a Japanese to US date converter which will take any text copied to the clipboard, search it for JP-formatted dates, and replace each date with its US-formatted equivalent. I have the search patterns and conversion calculations correct, but can't figure out how to have the US dates replace their JP dates in the original text.

As an example, when run through this macro, this sample text:

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et 平成20/04/30 dolore magna aliqua.
Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
12/5/16
Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.平成11年/05/15
平成25/7/4
平成13/01/30Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

should ideally end up like this:

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et 04/30/2008 dolore magna aliqua.
Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
5/16/2012
Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.05/15/1999
7/4/2013
01/30/2001Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

Or in other words, these dates (formatted Year/Month/Day):

平成20/04/30
12/5/16
平成11年/05/15
平成25/7/4
平成13/01/30

should change to these dates (formatted Month/Day/Year):

04/30/2008
5/16/2012
05/15/1999
7/4/2013
01/30/2001

As far as I can tell, I need another step after the "Search and Replace MatchDate Variable" action that writes the variable back to the original text in the same location (i.e. replacing the JP dates with US date equivalents) but I can't figure out what that should be. Any ideas as to how I can go about this would be much appreciated.

JP Dates → US Dates.kmmacros (8.8 KB)

P.S. For extra credit, if anyone could also tell me why my second switch action, which is supposed to convert all dates without 平成 in front of the year, is failing to work, that would be much appreciated as well.

I haven't tested this, but I think the problem is that you are replacing "DateMatch", which is just a temporary Variable.

You need to replace the text in the Clipboard, something like this:
(you will need to change or create the Variable "US_Date")

That is the general idea. You may need to make adjustments.

BTW, as a personal preference, I don't like working with the Clipboard, so I always set a variable to the clipboard, and then process the variable.

Thanks, @JMichaelTX! I knew I had to replace the clipboard with the new variable values, but couldn’t figure out how to do so without replacing all the regex matches at once, rather than one at a time. Fortunately, your advice led me to the solution: storing the match's initial state in a different variable at the start of the "For Each" loop, then searching the clipboard and replacing just the original match's variable with the new variable. Here's the finished macro, now expanded to include Showa dates (1926–1988) as well as Heisei (1989–present day):

JP Dates → US Dates.kmmacros (12.8 KB)

I like working with variables too, but I've also used macros that process the system clipboard in various ways for years with equal comfort. At one point in this macro's development I tried setting the results of the initial copy operation to a variable and processing it that way too, but in this case it seemed like an unnecessary step when processing the clipboard directly worked just as well.
Thanks again for your help!

This is a hole, IMO, in the KM "For Each" Action.
Thanks for inspiring me to think about this. So I have posted a question for Peter and others to help us with:

How Can I Do a Replace for EACH RegEx Match Found, as it is Found?

1 Like