I’m trying to use python’s Regular expression package re from within C++, within boost.python code. Here is an example code snippet in my C++ application:
#include <boost/python.hpp>
Py_Initialize();
object main = import("__main__");
object main_namespace = main.attr("__dict__");
object ignored = exec(
"import re\n"
"def run():\n"
" rmatch = re.search(r'\d',r'hello3')\n"
" print rmatch\n"
"\n"
"print 'main module loaded'\n", main_namespace);
object run_func = main.attr("run");
run_func();
Py_Finalize();
The regular expression should simply pick up the digit in the string hello3. This exact line of code works in Python, however in embedded python, rmatch is always None.
Would anyone be able to offer some insight as to why? Thanks!
I think you need to escape the backslash:
Remember it’s getting processed by the C++ compiler first. When Python gets hold of it, it will see
\dan a linefeed instead of\\dand\n. If you weren’t using Python’s raw strings (r''), you would have to write it as: