diff options
| author | Fredrik Lundh <fredrik@pythonware.com> | 2001-03-22 15:50:10 +0000 | 
|---|---|---|
| committer | Fredrik Lundh <fredrik@pythonware.com> | 2001-03-22 15:50:10 +0000 | 
| commit | b25e1ad253a4d96aea31a7a3fb78522ea354f43a (patch) | |
| tree | 2cc9dc18021270ffc2d7982ecca15b6942f59413 /Lib/sre.py | |
| parent | 8e9972c215ea0b10f0a7516d1cded6f26296ceba (diff) | |
| download | cpython-git-b25e1ad253a4d96aea31a7a3fb78522ea354f43a.tar.gz | |
sre 2.1b2 update:
- take locale into account for word boundary anchors (#410271)
- restored 2.0's *? behaviour (#233283, #408936 and others)
- speed up re.sub/re.subn
Diffstat (limited to 'Lib/sre.py')
| -rw-r--r-- | Lib/sre.py | 22 | 
1 files changed, 21 insertions, 1 deletions
| diff --git a/Lib/sre.py b/Lib/sre.py index 48d390a5ec..6706fac869 100644 --- a/Lib/sre.py +++ b/Lib/sre.py @@ -23,6 +23,8 @@ __all__ = [ "match", "search", "sub", "subn", "split", "findall",      "U", "IGNORECASE", "LOCALE", "MULTILINE", "DOTALL", "VERBOSE",      "UNICODE", "error" ] +__version__ = "2.1b2" +  # this module works under 1.5.2 and later.  don't use string methods  import string @@ -90,6 +92,7 @@ def compile(pattern, flags=0):  def purge():      "Clear the regular expression cache"      _cache.clear() +    _cache_repl.clear()  def template(pattern, flags=0):      "Compile a template pattern, returning a pattern object" @@ -111,6 +114,8 @@ def escape(pattern):  # internals  _cache = {} +_cache_repl = {} +  _MAXCACHE = 100  def _join(seq, sep): @@ -134,6 +139,21 @@ def _compile(*key):      _cache[key] = p      return p +def _compile_repl(*key): +    # internal: compile replacement pattern +    p = _cache_repl.get(key) +    if p is not None: +        return p +    repl, pattern = key +    try: +        p = sre_parse.parse_template(repl, pattern) +    except error, v: +        raise error, v # invalid expression +    if len(_cache_repl) >= _MAXCACHE: +        _cache_repl.clear() +    _cache_repl[key] = p +    return p +  def _expand(pattern, match, template):      # internal: match.expand implementation hook      template = sre_parse.parse_template(template, pattern) @@ -148,7 +168,7 @@ def _subn(pattern, template, string, count=0):      if callable(template):          filter = template      else: -        template = sre_parse.parse_template(template, pattern) +        template = _compile_repl(template, pattern)          def filter(match, template=template):              return sre_parse.expand_template(template, match)      n = i = 0 | 
