need REs at all, so there’s no need to bloat the language specification by including them.) Instead, the$_{re}$ module is simply a C extension module included with Python, just like the socket or zlib modules. Putting REs in strings keeps the Python language simpler, but has one disadvantage which is the topic of the next section. As stated earlier, regular expressions use the backslash character ($_{'\'}$ ) to indicate special forms or to allow special characters to be used without invoking their special meaning. This conflicts with Python’s usage of the same character for the same purpose in string literals. Let’s say you want to write a RE that matches the string$_{\section}$, which might be found in a LaTeX file. To figure out what to write in the program code, start with the desired string to be matched. Next, you must escape any backslashes and other metacharacters by preceding them with a backslash, resulting in the string \\section . The resulting string that must be passed to$_{re.compile()}$ must be$_{\\section}$. However, to express this as a Python string literal, both backslashes must be escaped again . 3.2 The Backslash Plague In short, to match a literal backslash, one has to write$_{'\\\\'}$ as the RE string, because the regular expression must be$_{\\}$, and each backslash must be expressed as$_{\\}$ inside a regular Python string literal. In REs that feature backslashes repeatedly, this leads to lots of repeated backslashes and makes the resulting strings difficult to understand. The solution is to use Python’s raw string notation for regular expressions; backslashes are not handled in any special way in a string literal prefixed with$_{'r'}$ , so$_{r"
"}$ is a two-character string containing$_{'\'}$ and$_{'n'}$ , while$_{"
"}$ is a one-character string containing a newline. Regular expressions will often be written in Python code using this raw string notation. In addition, special escape sequences that are valid in regular expressions, but not valid as Python string literals, now result in a$_{DeprecationWarning}$and will eventually become a$_{SyntaxError}$, which means the sequences will be invalid if raw string notation or escaping the backslashes isn’t used. Characters Stage \section Text string to be matched \\section $_{"\\\\section"}$Escaped backslashes for a string literal Regular String Raw string "ab*" r"ab*" "\\\\section" r"\\section" "\\w+\\s+\\1" 3.3 Performing Matches Once you have an object representing a compiled regular expression, what do you do with it? Pattern objects have several methods and attributes. Only the most significant ones will be covered here; consult the$_{re}$ docs for a complete listing. match() and$_{search()}$ return$_{None}$ if no match can be found. If they’re successful, a match object instance is returned, containing information about the match: where it starts and ends, the substring it matched, and more. Method/Attribute Purpose match() Determine if the RE matches at the beginning of the string. search() Scan through a string, looking for any location where this RE matches. findall() Find all substrings where the RE matches, and returns them as a list. finditer() Find all substrings where the RE matches, and returns them as an iterator. 5