Deitel & Associates, Inc. Logo

Back to www.deitel.com
digg.png delicious.png blinkit.png furl.png
Internet & World Wide Web How to Program, 3/e
Internet & World Wide Web How to Program, 3/e

ISBN:
0-13-145091-3
© 2004
pages: 1420

Order
Amazon logo

This tutorial continues our introduction to Python with basic string input and output capabilities, and an introduction to regular expression processing with the Python re module. This tutorial is intended for students and developers who are already familiar with basic Python programming or who have read our prior Python tutorials (see the list at the bottom of this page).
Download the Code Examples
[Note: This tutorial is an excerpt (Section 35.4) of Chapter 35, Python, from our textbook Internet & World Wide Web How to Program, 3/e. This tutorial may refer to other chapters or sections of the book that are not included here. Permission Information: Deitel, Harvey M. and Paul J., INTERNET & WORLD WIDE WEB HOW TO PROGRAM, 3/E, 2004, pp.1254-1259. Electronically reproduced by permission of Pearson Education, Inc., Upper Saddle River, New Jersey.]
35.4   String Processing and Regular Expressions (Continued)
Figure 35.12 presents some of Python's regular expression operations. Line 4 imports the re (regular expression) module. A module contains data and functions that a program can use to accomplish a specific task. After importing a module, the program can make use of the data and functions it contains. In our example, importing the re module enables us to access data and functions that facilitate regular-expression processing.
Line 8 compiles the regular expression "Test", using the re module's compile function. This method returns an object of type SRE_Pattern, which represents a compiled regular expression.
Fig. 35.12 Regular expressions to search a string.
1   # Fig. 35.12: fig35_12.py
2   # Program searches a string using the regular expression module.
3
4   import re
5
6   searchString = "Testing pattern matches"
7
8   expression1 = re.compile( r"Test" )
9   expression2 = re.compile( r"^Test" )
10   expression3 = re.compile( r"Test$" )
11   expression4 = re.compile( r"\b\w*es\b" )
12   expression5 = re.compile( r"t[aeiou]", re.I )
13
14   if expression1.search( searchString ):
15      print '"Test" was found.'
16
17   if expression2.match( searchString ):
18      print '"Test" was found at the beginning of the line.'
19
20   if expression3.match( searchString ):
21      print '"Test" was found at the end of the line.'
22
23   result = expression4.findall( searchString )
24
25   if result:
26      print 'There are %d words(s) ending in "es":' % \
27         ( len( result ) ),
28
29      for item in result:
30         print " " + item,
31
32   print
33   result = expression5.findall( searchString )
34
35   if result:
36      print 'The letter t, followed by a vowel, occurs %d times:' % \
37         ( len( result ) ),
38
39      for item in result:
40         print " " + item,
41
42   print

"Test" was found.
"Test" was found at the beginning of the line.
There are 1 words(s) ending in "es": matches
The letter t, followed by a vowel, occurs 3 times: Te ti te

Software Engineering Observation 35.1
If a program uses a regular expression string many times, compiling that string can speed up the regular expression comparisons.
Pages in this Tutorial:   1 | 2 | 3 | 4
 
Additional Python Tutorials:
Introduction to Python

Python Basic Data Types, Control Statements and Functions

Tuples, Lists and Dictionaries

Python CGI Programming

Return to Tutorial Index