Package: re2
ContentsSummaryBackwards CompatibilityInstallationUnicode SupportPerformanceCurrent StatusContactTestsMissing FeaturesCreditsSummarypyre2 is a Python extension that wrapsGoogle’s RE2 regular expression library.This version of pyre2 is similar to the one you’d
find atfacebook’s github repositoryexcept that the stated goal of this version is to be adrop-in replacementfor
theremodule.Backwards CompatibilityThe stated goal of this module is to be a drop-in replacement forre.
My hope is that some will be able to go to the top of their module and put:try:
    import re2 as re
except ImportError:
    import reThat being said, there are features of theremodule that this module may
never have. For example,RE2does not handle lookahead assertions ((?=...)).
For this reason, the module will automatically fall back to the originalremodule
if there is a regex that it cannot handle.However, there are times when you may want to be notified of a failover. For this reason,
I’m adding the single functionset_fallback_notificationto the module.
Thus, you can write:try:
    import re2 as re
except ImportError:
    import re
else:
    re.set_fallback_notification(re.FALLBACK_WARNING)And in the above example,set_fallback_notificationcan handle 3 values:re.FALLBACK_QUIETLY(default),re.FALLBACK_WARNING(raises a warning), andre.FALLBACK_EXCEPTION(which raises an exception).Note: The re2 module treats byte strings as UTF-8. This is fully backwards compatible with 7-bit ascii.
However, bytes containing values larger than 0x7f are going to be treated very differently in re2 than in re.
The RE library quietly ignores invalid utf8 in input strings, and throws an exception on invalid utf8 in patterns.
For example:>>> re.findall(r'.', '\x80\x81\x82')
['\x80', '\x81', '\x82']
>>> re2.findall(r'.', '\x80\x81\x82')
[]If you require the use of regular expressions over an arbitrary stream of bytes, then this library might not be for you.InstallationTo install, you must first install the prerequisites:There2 library from GoogleThe Python development headers (e.g.sudo apt-get install python-dev)A build environment withg++(e.g.sudo apt-get install build-essential)After the prerequisites are installed, you can try installing usingeasy_install:$ sudo easy_install re2if you have setuptools installed (or usepip).If you don’t want to usesetuptools, you can alternatively download the tarball frompypi.Alternative to those, you can clone this repository and try installing it from there. To do this, run:$ git clone git://github.com/axiak/pyre2.git
$ cd pyre2.git
$ sudo python setup.py installIf you want to make changes to the bindings, you must have Cython >=0.13.Unicode SupportOne current issue is Unicode support. As you may know,RE2supports UTF8,
which is certainly distinct from unicode. Right now the module will automatically
encode any unicode string into utf8 for you, which isslow(it also has to
decode utf8 strings back into unicode objects on every substitution or split).
Therefore, you are better off using bytestrings in utf8 while working with RE2
and encoding things after everything you need done is finished.PerformancePerformance is of course the point of this module, so it better perform well.
Regular expressions vary widely in complexity, and the salient feature ofRE2is
that it behaves well asymptotically. This being said, for very simple substitutions,
I’ve found that occasionally python’s regularremodule is actually slightly faster.
However, when theremodule gets slow, it getsreallyslow, while this module
buzzes along.In the below example, I’m running the data against 8MB of text from the collosal Wikipedia
XML file. I’m running them multiple times, being careful to use thetimeitmodule.
To see more details, please see theperformance script.TestDescription# total runsretime(s)re2time(s)%retimeregextime(s)%regextimeFindall URI|EmailFind list of ‘([a-zA-Z][a-zA-Z0-9]*)://([^ /]+)(/[^ ]*)?|([^ @]+)@([^ @]+)’219.9610.3361.68%11.4632.93%Replace WikiLinksThis test replaces links of the form [[Obama|Barack_Obama]] to Obama.10016.0322.62216.35%2.89590.54%Remove WikiLinksThis test splits the data by the <page> tag.10015.9831.4068.80%2.25262.43%Feel free to add more speed tests to the bottom of the script and send a pull request my way!Current Statuspyre2 has only received basic testing. Please use it
and let me know if you run into any issues!ContactYou can file bug reports on GitHub, or contact the author:Mike Axiak  contact page.TestsIf you would like to help, one thing that would be very useful
is writing comprehensive tests for this. It’s actually really easy:Come up with regular expression problems using the regular python ‘re’ module.Write a session in python traceback formatExample.Replace yourimport rewithimport re2 as re.Save it as a .txt file in the tests directory. You can comment on it however you like and indent the code with 4 spaces.Missing FeaturesCurrently the features missing are:If you use substitution methods without a callback, a non 0/1 maxsplit argument is not supported.CreditsThough I ripped out the code, I’d like to thank David Reiss
and Facebook for the initial inspiration. Plus, I got to
gut this readme file!Moreover, this library would of course not be possible if not for
the immense work of the team at RE2 and the few people who work
on Cython.