charref =re.compile( r""" &[#] # Start of a numeric entity reference ( 0[0-7]+ # Octal form | [0-9]+ # Decimal form | x[0-9a-fA-F]+ # Hexadecimal form ) ; # Trailing semicolon """ , re.VERBOSE) charref =re.compile( "&#(0[0-7]+ " "|[0-9]+ " "|x[0-9a-fA-F]+); ") 9 Some of the remaining metacharacters to be discussed are zero-width assertions . They don’t cause the engine to advance through the string; instead, they consume no characters at all, and simply succeed or fail. For example,$_{\b}$ is an assertion that the current position is located at a word boundary; the position isn’t changed by the$_{\b}$ at all. This means that zero-width assertions should never be repeated, because if they match once at a given location, they can obviously be matched an infinite number of times. There are some metacharacters that we haven’t covered yet. Most of them will be covered in this section. 4.1 More Metacharacters 4 More Pattern Power Without the verbose setting, the RE would look like this: In the above example, Python’s automatic concatenation of string literals has been used to break up the RE into smaller pieces, but it’s still more difficult to understand than the version using$_{re.VERBOSE}$. So far we’ve only covered a part of the features of regular expressions. In this section, we’ll cover some new metachar- acters, and how to use groups to retrieve portions of the text that was matched. | Alternation, or the “or” operator. If A and B are regular expressions,$_{A}$$_{|}$$_{B}$ will match any string that matches either A or B .$_{|}$ has very low precedence in order to make it work reasonably when you’re alternating multi-character strings.$_{Crow}$$_{|}$$_{Servo}$ will match either$_{'Crow'}$ or$_{'Servo'}$ , not$_{'Cro'}$ , a$_{'w'}$ or an$_{'S'}$ , and$_{'ervo'}$ . ^ Matches at the beginning of lines. Unless the$_{MULTILINE}$ flag has been set, this will only match at the beginning of the string. In$_{MULTILINE}$ mode, this also matches immediately after each newline within the string. To match a literal$_{'}$$_{|}$$_{'}$ , use$_{\}$$_{|}$ , or enclose it inside a character class, as in$_{[}$$_{|}$$_{]}$ . For example, if you wish to match the word$_{From}$ only at the beginning of a line, the RE to use is$_{^From}$ . For example, here’s a RE that uses re.VERBOSE ; see how much easier it is to read? This flag allows you to write regular expressions that are more readable by granting you more flexibility in how you can format them. When this flag has been specified, whitespace within the RE string is ignored, except when the whitespace is in a character class or preceded by an unescaped backslash; this lets you organize and indent the RE more clearly. This flag also lets you put comments within a RE that will be ignored by the engine; comments are marked by a$_{'#'}$ that’s neither in a character class or preceded by an unescaped backslash. Make$_{\w}$,$_{\W}$ ,$_{\b}$,$_{\B}$ ,$_{\s}$ and$_{\S}$ perform ASCII-only matching instead of full Unicode matching. This is only meaningful for Unicode patterns, and is ignored for byte patterns. X VERBOSE A ASCII