Is it possible to use re2 from Python?

前端 未结 4 1247
醉梦人生
醉梦人生 2021-02-07 01:24

i just discovered http://code.google.com/p/re2, a promising library that uses a long-neglected way (Thompson NFA) to implement a regular expression engine that can be orders of

相关标签:
4条回答
  • 2021-02-07 01:58

    Possible yes, easy no. Looking at the re2.h, this is a C++ library exposed as a class. There are two ways you could use it from Python.

    1.) As Tuomas says, compile it as a DLL/so and use ctypes. In order to use it from python, though, you would need to wrap the object init and methods into c style externed functions. I've done this in the past with ctypes by externing functions that pass a pointer to the object around. The "init" function returns a void pointer to the object that gets passed on each subsequent method call. Very messy indeed.

    2.) Wrap it into a true python module. Again those functions exposed to python would need to be extern "C". One option is to use Boost.Python, that would ease this work.

    0 讨论(0)
  • 2021-02-07 02:11

    SWIG handles C++ (unlike ctypes), so it may be more straightforward to use it.

    0 讨论(0)
  • 2021-02-07 02:13

    David Reiss has put together a Python wrapper for re2. It doesn't have all of the functionality of Python's re module, but it's a start. It's available here: http://github.com/facebook/pyre2.

    0 讨论(0)
  • 2021-02-07 02:21

    You could try to build re2 into its own DLL/so and use ctypes to call functions from that DLL/so. You will probably need to define your own entry points in the DLL/so.

    0 讨论(0)
提交回复
热议问题