Massive Search and Replace with Notepad++
   2 min read

In my article workflow I keep having to convert tags into Hugo shortcodes, and removing the escapes on all the characters Pandoc thinks it has to protect.
Doing this one by one is the type of work which has me running for some kind of automation - and that’s why I say I’m the laziest man in the world.
I tried to get some kind of plugin for Notepad++ or VSCode that would help me, but in the end they all had problems; namely, since my targets and my substitutions have many special characters, I wanted to avoid tools that used regular expressions.

Yes, *NIX was made for this kind of thing, and tools like sed would be perfect for what I want. Minus the regular expressions part…

I ended up realizing that I couldn’t find anything that did not use regular expressions. trombone.wav

Notepad++ solution

Eventually, after smashing my head against the wall, I realized that I could do this with the normal Notepad++ search, because it accepts PCRE regular expressions. And in PCRE, it’s possible to build a single expression made of many search and replacement chains, with this format:

Search Replacement
(ta)|(te)|(ti) (?1pa)(?2pe)(?3pi)

Tne issue of the many escapeable characters was solved with string literals, which I know from .NET as @"foo", but don’t really work as such in regular regex; however, the PCRE has string literals through the syntax \Qfoo\E
For the rest, capturing \_ implies escaping the \(\\) and the _(\_): the end result is \\\_
Then I still had to solve the problem of having to escape characters in the replacement; thankfully, on PCRE we only need to escape $, \, (, ), ?, e :

For example:

  • < e > come out of Pandoc as &lt; e &gt;:
  • removing the escapes from \,_,`,*,[, ], (.),|

Search

 (&gt;)|(&lt;)|(\\\\)|(\\\_)|(\\\`)|(\\\*)|(\\\[)|(\\\])|(\\\()|(\\\))|(\\\|)

Replacement

(?1>)(?2<)(?3\\)(?4_)(?5`)(?6*)(?7[)(?8])(?9\()(?10\))(?11|)

It does require a LOT of attention because not all the usual suspects need to be escaped on the way out…

With this functionality, I can use abbreviations for shortcodes and I accelerated immensely the preparation of these articles.
While what takes the longest is to learn and document, that’s also the fun part; converting my documentation into a publishable article should be as automatic and immediate as possible in order to continue to learn and document…

What's on this Page