Advertisement
Guest User

Untitled

a guest
May 25th, 2012
235
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.49 KB | None | 0 0
  1. #!/usr/bin/python
  2.  
  3. # P2PU - Python Programming 101 - Chapter 10 - Regular Expressions
  4. # Python for Informatics - Chapter 11 - Regular Expressions
  5. # Exercise 11.1
  6.  
  7. import re
  8.  
  9. def grep(regex, fhand):
  10.     matches = 0
  11.     for line in fhand:
  12.         line = line.rstrip()
  13.         if re.search(regex, line): matches += 1
  14.     return matches
  15.  
  16. regex = raw_input('Enter a regular expression: ')
  17. fname = 'mbox.txt'
  18. fhand = open(fname)
  19.  
  20. print "%s had %d lines that matched %s" % (fname, grep(regex, fhand), regex)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement