Using std::regex causes plugin loading to fail — is there an alternative?
While working on a plugin using Visual Studio 2022, I noticed that when I added some std::regex code, the plugin build was successful but Notepad++ could not load it. Removing the std::regex code made it work again.
I’m guessing this has something to do with the statement in build . md of Notepad++ that “Notepad++ is always built with Boost regex PCRE support instead of default c++11 regex ECMAScript used by plain Scintilla.” But the implications of that are beyond my understanding of C++ linkage and libraries.
Is there a way to use regular expressions in a C++ plugin for Notepad++? For my purposes, it probably doesn’t have to be std::regex, though I’ve never used the Boost libraries and have no idea how they work.
I’m guessing this has something to do with the statement in build . md of Notepad++ that “Notepad++ is always built with Boost regex PCRE support instead of default c++11 regex ECMAScript used by plain Scintilla.”
Boost is just the regex engine for the editor’s find & replace capabilities. There’s no requirement to use any Boost APIs in a plugin. (You would have to copy the Boost header files into your own project to make that possible).
Removing the std::regex code made it work again.
The reason why is basically what you quoted from
BUILD.md. Notepad++ “turns off” C++11’s builtin regex support in favour of Boost, as explained here:
The C++11 <regex> support may be disabled by compiling Scintilla with NO_CXX11_REGEX defined.
A simple interface provides support for switching the Regular Expressions engine at compile time.
. . .
Preprocessors affect building
Preprocessor Description NO_CXX11_REGEX Build Scintilla without C++11 std::regex.
Is there a way to use regular expressions in a C++ plugin for Notepad++?
Scintilla’s APIs may be enough to get the job done, even if they are “limited and should only be used for simple cases and initial development”.
You could try using re2, which is a regex engine by Google, much faster than std::regex, and is available in vcpkg. I have used it in a plugin myself.
All of Boost is also available in vcpkg, so if you’re unsure how to install Boost that is the best way to go.
It is pretty easy to set up vcpkg for use with Visual Studio, if you haven’t already. If you have trouble, I can provide some help, but give it a try first.
easy to set up vcpkg for use with Visual Studio, if you haven’t already. If you have trouble, I can provide some help
I’d think there might be better, more on-topic, forums for that, but let’s see how it goes.
The problem was not at all what I thought. It turned out to be ordinary user error. There was a fault in the syntax of my regular expression. Having never used C++ regular expressions before, I didn’t know that even when the expression is coded as a character literal, Visual Studio compiles code containing invalid regular expressions without complaint and then throws a run-time error. Because it was in a plug-in dll and it was a static constant (hence, processed on loading), the dll failed to load at all.
There is no problem loading a plugin that uses std::regex, so long as the expression doesn’t cause an exception.
Thanks to those who replied.