Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- h = re.compile('hello')
- h.match('hello world')
- re.match('hello', 'hello world')
- def match(pattern, string, flags=0):
- return _compile(pattern, flags).match(string)
- def _compile(*key):
- # Does cache check at top of function
- cachekey = (type(key[0]),) + key
- p = _cache.get(cachekey)
- if p is not None: return p
- # ...
- # Does actual compilation on cache miss
- # ...
- # Caches compiled regex
- if len(_cache) >= _MAXCACHE:
- _cache.clear()
- _cache[cachekey] = p
- return p
- num = "..."
- # then, much later:
- m = re.match(num, input)
- num = re.compile("...")
- # then, much later:
- m = num.match(input)
- $ python -m timeit -s "import re" "re.match('hello', 'hello world')"
- 100000 loops, best of 3: 3.82 usec per loop
- $ python -m timeit -s "import re; h=re.compile('hello')" "h.match('hello world')"
- 1000000 loops, best of 3: 1.26 usec per loop
- foo_pat = re.compile('foo',re.I)
- foo_pat.findall('some string FoO bar')
- >> ['FoO']
- re.findall('(?i)foo','some string FoO bar')
- >> ['FoO']
- import re
- import time
- rgx = re.compile('(w+)s+[0-9_]?s+w*')
- str = "average 2 never"
- a = 0
- t = time.time()
- for i in xrange(1000000):
- if re.match('(w+)s+[0-9_]?s+w*', str):
- #~ if rgx.match(str):
- a += 1
- print time.time() - t
- import re
- import time
- rgx = re.compile('(w+)s+[0-9_]?s+w*')
- str = "average 2 never"
- a = 0
- t = time.time()
- for i in xrange(1000000):
- if re.match('(w+)s+[0-9_]?s+w*', str):
- #~ if rgx.match(str):
- a += 1
- print time.time() - t
- import re
- import time
- rgx = re.compile('(w+)s+[0-9_]?s+w*')
- str = "average 2 never"
- a = 0
- t = time.time()
- for i in xrange(1000000):
- if re.match('(w+)s+[0-9_]?s+w*', str):
- #~ if rgx.match(str):
- a += 1
- print time.time() - t
- import re
- import time
- import fpformat
- Regex1 = re.compile('^(a|b|c|d|e|f|g)+$')
- Regex2 = re.compile('^[a-g]+$')
- TimesToDo = 1000
- TestString = ""
- for i in range(1000):
- TestString += "abababdedfg"
- StartTime = time.time()
- for i in range(TimesToDo):
- Regex1.search(TestString)
- Seconds = time.time() - StartTime
- print "Alternation takes " + fpformat.fix(Seconds,3) + " seconds"
- StartTime = time.time()
- for i in range(TimesToDo):
- Regex2.search(TestString)
- Seconds = time.time() - StartTime
- print "Character Class takes " + fpformat.fix(Seconds,3) + " seconds"
- Alternation takes 2.299 seconds
- Character Class takes 0.107 seconds
- import re
- import time
- import fpformat
- TimesToDo = 1000
- TestString = ""
- for i in range(1000):
- TestString += "abababdedfg"
- StartTime = time.time()
- for i in range(TimesToDo):
- re.search('^(a|b|c|d|e|f|g)+$',TestString)
- Seconds = time.time() - StartTime
- print "Alternation takes " + fpformat.fix(Seconds,3) + " seconds"
- StartTime = time.time()
- for i in range(TimesToDo):
- re.search('^[a-g]+$',TestString)
- Seconds = time.time() - StartTime
- print "Character Class takes " + fpformat.fix(Seconds,3) + " seconds"
- Alternation takes 2.508 seconds
- Character Class takes 0.109 seconds
- """ Re.py: Re.match = re.match + cache
- efficiency: re.py does this already (but what's _MAXCACHE ?)
- readability, inline / separate: matter of taste
- """
- import re
- cache = {}
- _re_type = type( re.compile( "" ))
- def match( pattern, str, *opt ):
- """ Re.match = re.match + cache re.compile( pattern )
- """
- if type(pattern) == _re_type:
- cpat = pattern
- elif pattern in cache:
- cpat = cache[pattern]
- else:
- cpat = cache[pattern] = re.compile( pattern, *opt )
- return cpat.match( str )
- # def search ...
- from re import compile as _Re
- class TYPO:
- def text_has_foobar( self, text ):
- return self._text_has_foobar_re_search( text ) is not None
- _text_has_foobar_re_search = _Re( r"""(?i)foobar""" ).search
- TYPO = TYPO()
- from TYPO import TYPO
- print( TYPO.text_has_foobar( 'FOObar ) )
- import re
- class Typo:
- def text_has_foobar( self, text ):
- return re.compile( r"""(?i)foobar""" ).search( text ) is not None
- typo = Typo()
- print( typo.text_has_foobar( 'FOObar ) )
Add Comment
Please, Sign In to add comment