Showmedo is financed by a subscription club, membership of which grants access to all of our 160+ specially produced videos. These include our 50 video introduction to Python programming, and our recently completed Open-office migration collection.
 Jeff Rush
Greetings. I'm the (former) Python Advocacy Coordinator and a strong supporter of screencasts. I'm also the organizer of the Dallas-Ft. Worth Pythoneers and was con chair for PyCon 2006 and 2007 in Dallas.
I'm also an independent consultant and work in the areas of Python/Zope, embedded Linux s ...
Walkthru of a Simple Text Line Matcher (grep) [ID:368] (1/2)
 Jeff Rush
Greetings. I'm the (former) Python Advocacy Coordinator and a strong supporter of screencasts. I'm also the organizer of the Dallas-Ft. Worth Pythoneers and was con chair for PyCon 2006 and 2007 in ...
Our authors tell us that feedback from you is a big motivator. Please take a few moments to let them know what you think of their work.
(Showmedo is undergoing major changes. To
report any problems viewing the videos please
email us and include
browser and OS specifics. Cheers - Kyran.)
Let's start with a simple program, one that reads lines of text from one or more files and prints out those lines that match a regular-expression pattern. We'll give it command-line options and then in successive reworkings in this talk have it underline the matches and accept multiple patterns any of which can match.
A simple introduction to Python programming for those just getting started.
"""GREP-Like Print Lines Matching One or More Given Patterns
Search one or more named input files (standard
input if no files are named, or the file name '-'
is given) for lines containing a match to the
given set of patterns. If any one of them
matches, print the line.
from optparse import OptionParser
"""A Set of Searchable Regular Expression Patterns
Accept one or more regular expressions such
that if any one of them matches a line the
first successful match is returned.
def __init__(self, multipatterns):
self.multipatterns = [ re.compile(pattern)
for pattern in multipatterns ]
def search(self, line):
for pattern in self.multipatterns:
match = pattern.search(line)
if match is not None:
parser = OptionParser(
usage="%prog [-u] -e PATTERN FILELIST",
help="pattern expression on which to match")
action="store_true", dest="underscore", default=False,
help="draw underscores under the matching text")
options, args = parser.parse_args()
if options.regexpr is None:
parser.error("missing pattern against which to match")
matcher = MultiMatcher(options.regexpr)
for line in fileinput.input(args):
line = line[:-1]
match = matcher.search(line)
start, end = match.start(), match.end()
prefix = "%s:%d " % (
print prefix + line
print prefix \
+ " " * start \
+ "-" * (end - start)
if __name__ == "__main__":
Are you learning Python? To keep the site running clean and ad-free we've started Club ShowMeDo. The club will allow you access to some special videos we're making to pass on programming skills we consider important to the development and enjoyment of Free Open Source Software (FOSS).
The focus at the moment is on developing with the Python programming language, one of the backbones of the FOSS movement with a huge number of included and third-party libraries.
We welcome feedback and we want to hear your ideas.
Showmedo's development is fairly rapid and bugs will inevitably creep in. If you have any problems please drop us a line using the contact address below. Likewise, any suggestions for improvements to the site are gratefully received.