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.
Constraining the Search
In situations where it is known in advance that only a subset of the full input should be searched, the regular expression match can be further constrained by telling
re to limit the search range. For example, if the pattern must appear at the front of the input, then using
match() instead of
search() will anchor the search without having to explicitly include an anchor in the search pattern.
Since the literal text
is does not appear at the start of the input text, it is not found using
match(). The sequence appears two other times in the text, though, so
search() finds it.
fullmatch() method requires that the entire input string match the pattern.
search() shows that the pattern does appear in the input, but it does not consume all of the input so
fullmatch() does not report a match.
search() method of a compiled regular expression accepts optional
end position parameters to limit the search to a substring of the input.
This example implements a less efficient form of
iterall(). Each time a match is found, the end position of that match is used for the next search.