Advertisement
rfmonk

mmap_regex.py

Jan 28th, 2014
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.52 KB | None | 0 0
  1. #1/usr/bin/env python
  2.  
  3. # this is from The Python
  4. # Standard Library by example
  5. # ISBN13: 9780321767349
  6.  
  7. import mmap
  8. import re
  9. import contextlib
  10.  
  11. pattern = re.compile(r'(\.\W+)?([^.]?nulla[^.]*?\.)',
  12.                      re.DOTALL | re.IGNORECASE | re.MULTILINE)
  13.  
  14. with open('lorem.txt', 'r') as f:
  15.     with contextlib.closing(mmap.mmap(f.fileno(), 0,
  16.                                       access=mmap.ACCESS_READ)
  17.                             ) as m:
  18.         for match in pattern.findall(m):
  19.             print match[1].replace('\n', ' ')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement