Abstract This document is an introductory tutorial to using regular expressions in Python with the$_{re}$ module. It provides a gentler introduction than the corresponding section in the Library Reference. 1 Introduction 2 Simple Patterns 2.1 Matching Characters The first metacharacters we’ll look at are$_{[}$ and$_{]}$. They’re used for specifying a character class, which is a set of characters that you wish to match. Characters can be listed individually, or a range of characters can be indicated by giving two characters and separating them by a$_{'-'}$ . For example,$_{[abc]}$will match any of the characters$_{a}$,$_{b}$, or $_{c}$; this is the same as$_{[a-c]}$ , which uses a range to express the same set of characters. If you wanted to match only lowercase letters, your RE would be$_{[a-z]}$. Metacharacters are not active inside classes. For example,$_{[akm$]}$will match any of the characters$_{'a'}$ ,$_{'k'}$ , 'm' , or$_{'$'}$ ;$_{'$'}$ is usually a metacharacter, but inside a character class it’s stripped of its special nature. Here’s a complete list of the metacharacters; their meanings will be discussed in the rest of this HOWTO. There are exceptions to this rule; some characters are special metacharacters , and don’t match themselves. Instead, they signal that some out-of-the-ordinary thing should be matched, or they affect other portions of the RE by repeating them or changing their meaning. Much of this document is devoted to discussing various metacharacters and what they do. Most letters and characters will simply match themselves. For example, the regular expression$_{test}$will match the string$_{test}$exactly. (You can enable a case-insensitive mode that would let this RE match$_{Test}$ or$_{TEST}$ as well; more about this later.) For a detailed explanation of the computer science underlying regular expressions (deterministic and non- deterministic finite automata), you can refer to almost any textbook on writing compilers. We’ll start by learning about the simplest possible regular expressions. Since regular expressions are used to operate on strings, we’ll begin with the most common task: matching characters. The regular expression language is relatively small and restricted, so not all possible string processing tasks can be done using regular expressions. There are also tasks that can be done with regular expressions, but the expressions turn out to be very complicated. In these cases, you may be better off writing Python code to do the processing; while Python code will be slower than an elaborate regular expression, it will also probably be more understandable. Regular expression patterns are compiled into a series of bytecodes which are then executed by a matching engine written in C. For advanced use, it may be necessary to pay careful attention to how the engine will execute a given RE, and write the RE in a certain way in order to produce bytecode that runs faster. Optimization isn’t covered in this document, because it requires that you have a good understanding of the matching engine’s internals. Regular expressions (called REs, or regexes, or regex patterns) are essentially a tiny, highly specialized programming language embedded inside Python and made available through the$_{re}$ module. Using this little language, you specify the rules for the set of possible strings that you want to match; this set might contain English sentences, or e-mail addresses, or TeX commands, or anything you like. You can then ask questions such as “Does this string match the pattern?”, or “Is there a match for the pattern anywhere in this string?”. You can also use REs to modify a string or to split it apart in various ways. 2 . ^ $ * + ? { } [ ] \ | ( )