Last active
September 20, 2016 14:13
-
-
Save harjitmoe/be3a9a1a9544ec952ba487813a774829 to your computer and use it in GitHub Desktop.
Simple, very classic Python RegExp API for modern Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
r"""Older, simpler interface for simple RegEx operations. | |
Simple regular expression interface compatible with historic regexp | |
module (based on the regexp.py from Python 1.1, which was included | |
without modification in subseqent versions before eventual | |
deletion). | |
Ported by Thomas Hori to use the "re" API rather than the previous | |
"regex" module from Python 1.x as its backend. | |
Due to changes in engine, while this still supports \b \B \w \W | |
as in Python 0.9.5 onwards, backslashed `'<> probably do not | |
carry special meaning (it does, however, support \A and \Z). | |
""" | |
import re | |
class Prog: | |
def __init__(self, pat): | |
self.prog = re.compile(pat, re.M) | |
def match(self, str, offset = 0): | |
s = self.prog.search(str, offset) | |
if not s: | |
return [] | |
regs = [] | |
for i in range(len(s.groups())+1): | |
regs.append(s.span(i)) | |
i = len(regs) | |
while i > 0 and regs[i-1] == (-1, -1): | |
i = i-1 | |
return regs[:i] | |
cache_pat = None | |
cache_prog = None | |
def match(pat, str): | |
global cache_pat, cache_prog | |
if pat != cache_pat: | |
cache_pat, cache_prog = pat, compile(pat) | |
return cache_prog.match(str) | |
setattr(Prog,"exec",Prog.match) | |
error = re.error | |
compile = Prog | |
globals()["exec"] = match | |
__copying__ = """ | |
Based on a file from Python 1.1, which is licensed as below. | |
Modifications by Thomas Hori may be used etc under the same conditions. | |
Copyright 1991, 1992, 1993, 1994 by Stichting Mathematisch Centrum, | |
Amsterdam, The Netherlands. | |
All Rights Reserved | |
Permission to use, copy, modify, and distribute this software and its | |
documentation for any purpose and without fee is hereby granted, | |
provided that the above copyright notice appear in all copies and that | |
both that copyright notice and this permission notice appear in | |
supporting documentation, and that the names of Stichting Mathematisch | |
Centrum or CWI not be used in advertising or publicity pertaining to | |
distribution of the software without specific, written prior permission. | |
STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO | |
THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND | |
FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE | |
FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | |
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | |
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT | |
OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | |
""" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment