Giter VIP home page Giter VIP logo

cffi_re2's Introduction

cffi_re2

Build Status

cffi_re2 is a cffi-based high-level Python binding for Google's re2 library.

Installation

Before installing cffi_re2, you will need to install re2. On Ubuntu/Debian you can do that using

sudo apt-get install libre2-dev

Else, you can simply install the current version from the git repository:

git clone https://github.com/google/re2.git
make
make test
sudo make install

See the re2 repository for further information.

After installing re2, you can install cffi_re2:

pip install cffi_re2

or from a local copy:

sudo python setup.py install

cffi_re2 is fully compatbile with both Python3.x as well as PyPy (including PyPy3).

You can run the unit tests using:

sudo python setup.py test

Using cffi_re2

cffi_re2 is mostly compatible to the re module from the Python standard library and exposes the same interface. In almost all cases you can use the same source code for both libraries. The flags in cffi_re2 are exactly the same as those in re, so you can e.g. use re.IGNORECASE in cffi_re2.compile and vice versa.

One way to use cffi_re2 is:

import cffi_re2 as re

Note however, that, due to the design of the RE2 library, some syntax elements like zero-width lookaheads or lookbehinds are not supported.

When using those syntax elements, the backend reports a syntax error when calling cffi_re2.compile, for example:

ValueError: invalid perl operator: (?<

One workaround is to convert your regex into a group-capturing form and select the appropriate group later. For larger sets of complex regular expressions, this is often not feasible, however.

In this case, it is recommended to use a hybrid approach, i.e. falling back to re if cffi_re2 fails to compile an expression.

import re
import cffi_re2

def compileRegex(rgx, flags=0):
    try:
        return cffi_re2.compile(rgx, flags)
    except ValueError:
        return re.compile(rgx, flags)

Note that in the current implementation there are still several known and unknown incompatibilities between cffi_re2 and re. If you encounter issues, please report them as a bug.

Benchmarks

About

cffi_re2 was originally developed by Liang Zhaohao. Many new features and improvements were contributed by Uli Köhler.

The library is licensed under the MIT license (see LICENSE file).

cffi_re2's People

Contributors

ulikoehler avatar vls avatar netease-games-opd-ai avatar pombredanne avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.