content_image/825929.jpg
<img src='content_image/825929.jpg'>

The end of the RE has now been reached, and it has matched 'abcb' . This demonstrates how the matching engine goes as far as it can at first, and if no match is found it will then progressively back up and retry the rest of the RE again and again. It will back up until it has tried zero matches for [bcd]* , and if that subsequently fails, the engine will conclude that the string doesn’t match the RE at all. 

Another repeating metacharacter is$_{+}$ , which matches one or more times. Pay careful attention to the difference between$_{*}$and$_{+}$ ;$_{*}$matches zero or more times, so whatever’s being repeated may not be present at all, while$_{+}$ requires at least one occurrence. To use a similar example,$_{ca+t}$ will match$_{'cat'}$ (1$_{'a'}$ ),$_{'caaat'}$ (3$_{'a'}$ s), but won’t match 'ct' . 

There are two more repeating qualifiers. The question mark character, ? , matches either once or zero times; you can think of it as marking something as being optional. For example, home-?brew matches either 'homebrew' or 'home-brew' . 

The most complicated repeated qualifier is$_{{m,n}}$, where m and n are decimal integers. This qualifier means there must be at least m repetitions, and at most n . For example,$_{a/{1,3}b}$ will match$_{'a/b'}$ ,$_{'a//b'}$ , and$_{'a///b'}$ . It won’t match$_{'ab'}$ , which has no slashes, or$_{'a////b'}$ , which has four. 

You can omit either m or n ; in that case, a reasonable value is assumed for the missing value. Omitting m is interpreted as a lower limit of 0, while omitting n results in an upper bound of infinity. 

Readers of a reductionist bent may notice that the three other qualifiers can all be expressed using this notation.$_{{0,}}$ is the same as$_{*}$ ,$_{{1,}}$is equivalent to$_{+}$ , and$_{{0,1}}$ is the same as$_{?}$ . It’s better to use$_{*}$ ,$_{+}$ , or$_{?}$ when you can, simply because they’re shorter and easier to read. 

## 3 Using Regular Expressions 

Now that we’ve looked at some simple regular expressions, how do we actually use them in Python? The$_{re}$ module provides an interface to the regular expression engine, allowing you to compile REs into objects and then perform matches with them. 

## 3.1 Compiling Regular Expressions 

Regular expressions are compiled into pattern objects, which have methods for various operations such as searching for pattern matches or performing string substitutions. 

>>> import re >>> p =re.compile( 'ab*') >>> p re.compile('ab*') 

re.compile() also accepts an optional flags argument, used to enable various special features and syntax varia- tions. We’ll go over the available settings later, but for now a single example will do: 

>>> p =re.compile( 'ab*', re.IGNORECASE) 

The RE is passed to$_{re.compile()}$ as a string. REs are handled as strings because regular expressions aren’t part of the core Python language, and no special syntax was created for expressing them. (There are applications that don’t 

