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.