The subject of regular expressions is quite deep, and it takes an immense amount of practice to get used to the special character syntax. Furthermore, the re module contains a vast set of methods available for performing searches using regular expressions. Upon completing the examples in this section, you should have a much deeper appreciation for how powerful regular expressions can be.
Input with Multiple Lines
Two flags affect how searching in multi-line input works:
MULTILINE flag controls how the pattern matching code processes anchoring instructions for text containing newline characters. When multiline mode is turned on, the anchor rules for
$ apply at the beginning and end of each line, in addition to the entire string.
The pattern in the example matches the first or last word of the input. It matches
line. at the end of the string, even though there is no newline.
DOTALL is the other flag related to multiline text. Normally, the dot character (
.) matches everything in the input text except a newline character. The flag allows the dot to match newlines as well.
Without the flag, each line of the input text matches the pattern separately. Adding the flag causes the entire string to be consumed.