Use Variables as Regex Strings in Search and Replace


I am looking to create a macro that has a user prompt for search / replace regex terms in a text file.

  • Ideally, the macro would prompt the user to enter the search and replace terms, and save the map variables.
  • The variables get put into the Search and Replace block (see 1). This does not work.
  • If I instead hard code the search and replace terms in the same block (see 2), then it will work.

What am I doing wrong? How can put dynamically created regex search terms into the macro?


For reference sake, here is a copy of the macro that I am using
Regex Search and replace in folder of text files.kmmacros (15.2 KB)

Sorry all, the last macro I attached had a bug (extra prompt box).

This version should function better.
4 Regex Search and replace in folder of text files.kmmacros (13.6 KB)

The discussion in Rename Files Mystery shows a prompt for both search and replacement strings for a filename that is subsequently executed by either Perl or JavaScript code. Which you might find illuminating.

You might also find interesting Textcavator 2021, which optionally takes a regex search and performs it on a set of files.

Thank you @mrpasini, for your help! That clears up a lot of what I was struggling with. I will try the Textcavator 2021 macro.

Thanks again :pray::smiley:

Glad those macros helped!