The caret character (^) defines a pattern that starts at the beginning of a line of text in the data stream.
If the pattern is located anyplace other than the start of the line of text, the regular expression pattern
fails.
To use the caret character, you must place it before the pattern specified in the regular expression,
like this:
Click here to view code image
>>> re.search('^book', 'The book store')
>>> re.search('^Book', 'Books are great')
<_sre.SRE_Match object at 0x015F9988>
>>>
The caret anchor character checks for the pattern at the beginning of each string, not each line. If you
need to match the beginning of each line of text, you need to use the MULTILINE feature of the
compiled regular expression, as in this example:
Click here to view code image
>>> re.search('^test', 'This is a\ntest of a new line')
>>>
>>> pattern = re.compile('^test', re.MULTILINE)
>>> pattern.search('This is a\ntest of a new line')
<_sre.SRE_Match object at 0x015F9988>
>>>
In the first example, the pattern doesn’t match the word test at the start of the second line in the text.
In the second example, using the MULTILINE feature, it does.
By the Way: Caret Versus match()
You’ll notice that the caret special character does the same thing as the match()
function. They’re interchangeable when you’re working with scripts.
Looking for the Ending
The opposite of looking for a pattern at the start of a line is looking for a pattern at the end of a line.
The dollar sign ($) special character defines the end anchor. You can add this special character after
a text pattern to indicate that the line of data must end with the text pattern, as in this example:
Click here to view code image
>>> re.search('book$', 'This is a good book')
<_sre.SRE_Match object at 0x015F99F8>
>>> re.search('book$', 'This book is good')
>>>
The problem with an ending text pattern is that you must be careful of what you’re looking for, as
shown here:
Click here to view code image
>>> re.search('book$', 'There are a lot of good books')
>>>
Because the book word is plural at the end of the line, it no longer matches the regular expression
pattern, even though book is in the data stream. The text pattern must be the very last thing on the line