Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python
- # P2PU - Python Programming 101 - Chapter 10 - Regular Expressions
- # Python for Informatics - Chapter 11 - Regular Expressions
- # Exercise 11.1
- import re
- def grep(regex, fhand):
- matches = 0
- for line in fhand:
- line = line.rstrip()
- if re.search(regex, line): matches += 1
- return matches
- regex = raw_input('Enter a regular expression: ')
- fname = 'mbox.txt'
- fhand = open(fname)
- print "%s had %d lines that matched %s" % (fname, grep(regex, fhand), regex)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement