‘^test’ 表示匹配以test开始的字符串
‘\d$’ 表示匹配以数字结尾的字符串
示例代码:
# 插入字符 ^
>>> import re
>>> testReg = re.compile(r'^test')
>>> test = testReg.search('test message end with 123')
>>> test
<re.Match object; span=(0, 4), match='test'>
>>> test.group()
'test'
# 美元字符$
>>> import re
>>> testReg = re.compile(r'\d$')
>>> test = testReg.search('test message end with 123')
>>> test
<re.Match object; span=(24, 25), match='3'>
>>> test2 = testReg.search('test message end with 123.')
>>> test2
>>> test2.group()
Traceback (most recent call last):
File "<pyshell#167>", line 1, in <module>
test2.group()
AttributeError: 'NoneType' object has no attribute 'group'
>>>