16.01.2014 Views

Beginning Python - From Novice to Professional

Beginning Python - From Novice to Professional

Beginning Python - From Novice to Professional

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

CHAPTER 10 ■ BATTERIES INCLUDED 235<br />

This interaction isn’t terribly interesting. I could have done exactly the same thing with an ordinary dictionary instead<br />

of the shelf object. But now that I’ve quit the program, let’s see what happens when I restart it—perhaps the<br />

following day?<br />

Enter command (? for help): lookup<br />

Enter ID number: 001<br />

What would you like <strong>to</strong> know? (name, age, phone) name<br />

Name: Mr. Gumby<br />

Enter command (? for help): quit<br />

As you can see, the program reads in the file I created the first time, and Mr. Gumby is still there!<br />

Feel free <strong>to</strong> experiment with this program, and see if you can extend its functionality and improve its user-friendliness.<br />

Perhaps you can think of a version that you have use for yourself? How about a database of your record collection?<br />

Or a database <strong>to</strong> help you keep track of which friends have borrowed which of your books? (I know I could use that<br />

last one.)<br />

re<br />

Some people, when confronted with a problem, think “I know, I’ll use regular<br />

expressions.” Now they have two problems.<br />

—Jamie Zawinski<br />

The re module contains support for regular expressions. If you’ve heard about regular expressions<br />

before, you probably know how powerful they are; if you haven’t, prepare <strong>to</strong> be amazed.<br />

You should note, however, that mastering regular expressions may be a bit tricky at first.<br />

(Okay, very tricky, actually.) The key is <strong>to</strong> learn about them a little bit at a time—just look up (in<br />

the documentation) the parts you need for a specific task. There is no point in memorizing it all<br />

up front. This section describes the main features of the re module and regular expressions,<br />

and enables you <strong>to</strong> get started.<br />

■Tip In addition <strong>to</strong> the standard documentation, Andrew Kuchling’s “Regular Expression HOWTO”<br />

(http://amk.ca/python/how<strong>to</strong>/regex/) is a useful source of information on regular expressions<br />

in <strong>Python</strong>.<br />

What Is a Regular Expression?<br />

A regular expression (also called a regex or regexp) is a pattern that can match a piece of text.<br />

The simplest form of regular expression is just a plain string, which matches itself. In other<br />

words, the regular expression 'python' matches the string 'python'. You can use this matching<br />

behavior for such things as searching for patterns in a text, for replacing certain patterns with<br />

some computed values, or for splitting a text in<strong>to</strong> pieces.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!