1 import re 2 # 匹配 qq 邮箱,5-10 位数字 3 qq = '8782303@qq.com' 4 # <re.Match object; span=(0, 14), match='8782303@qq.com'> 5 qq = '8782303@qq.cn' # None 6 # 必须限制结尾 7 pattern = r'[1-9]d{4,9}@qq.com$' # 结尾$原生字符串 8 v = re.match(pattern,qq) 9 print(v) 10 11 print('----------^开始----------') 12 s = 'hello world' 13 # s = 'python' # None 14 pattern = r'^hello.*' 15 # <re.Match object; span=(0, 11), match='hello world'> 16 o = re.match(pattern,s) 17 print(o) 18 19 print('-----------\b匹配单词的左边界----------') 20 pattern = r'.*ab' 21 # ab 左边界的情况 22 v = re.match(pattern,'123 abeer') 23 print(v) 24 # <re.Match object; span=(0, 6), match='123 ab'> 25 26 print('-------------\b匹配单词的右边界----------') 27 pattern = r'.*ab' 28 s = '12345 cab' 29 o = re.match(pattern,s) 30 print(o) 31 # <re.Match object; span=(0, 9), match='12345 cab'> 32 33 print('-------------\B匹配非单词的右边界----------') 34 pattern = r'.*abB' 35 s = '12345 abc' 36 o = re.match(pattern,s) 37 print(o) 38 # <re.Match object; span=(0, 8), match='12345 ab'>