Summary: in this tutorial, you’ll learn how to use regular expression anchors to match the character positions including the beginning and the end of a string.
Introduction to the regex anchors
Regular expressions provide you with two anchors that match the positions of characters:
^
– the caret anchor matches at the beginning of a string.$
– the dollar anchor matches at the end of a string.
The following example uses the \d\d
to match two digits in a time string:
import re time = '12:20' matches = re.finditer('\d\d', time) for match in matches: print(match.group())
Code language: JavaScript (javascript)
It returns two matches:
12 20
If you use the caret anchor (^
), you’ll get one group which is the two digits at the beginning of the string. For example:
import re time = '12:20' matches = re.finditer('^\d\d', time) for match in matches: print(match.group())
Code language: JavaScript (javascript)
Output:
12
Similarly, if you use the $
anchor, you’ll get the last two digits because the $
matches \d\d
at the end of the time string:
import re time = '12:20' matches = re.finditer('\d\d$', time) for match in matches: print(match.group())
Code language: JavaScript (javascript)
Output:
20
To check if a string is a time string, you can combine the caret (^
) and dollar ($
) anchors. For example:
import re time = '12:20' matches = re.finditer('^\d\d:\d\d$', time) for match in matches: print(match.group())
Code language: JavaScript (javascript)
Output:
12:20
Code language: CSS (css)
Note that the pattern ^\d\d:\d\d$
doesn’t validate the valid hour and minute. For example, it also matches the following string:
30:99
Code language: CSS (css)
It’s not a valid time string because the valid hour is from 1 to 24 and the valid minute is from 00 to 59. Later, you’ll learn how to match the time string with valid values using the alternation.
Leave a Reply