首页 Python通用模块视频教程 Python re模块-正则表达式字符边界匹配
pay pay

Python re模块-正则表达式字符边界匹配

日期: 五月 4, 2023, 10:18 a.m.
阅读: 176
作者: Python自学网-村长

摘要: Python re模块-正则表达式字符边界匹配

import re
# 匹配一个电话号码,首位是1,第二位是356789
# ^ 字符串开头
print(re.match('^1[35-9]\d{9}', 'a13955006789aa'))  # match方法自定锁定第一位匹配,所以^号在这里没用
print(re.findall('^1[35-9]\d{9}', 'a13955006789aa'))
# $ 字符串结尾
print(re.match('1[35-9]\d{9}', '13955006789aa'))
print(re.match('1[35-9]\d{9}$', '13955006789aa'))
# \b 单词边界
# \B 非单词边界
str1 = 'I put a lighted match to the letter and watched it burn.'
print(re.findall('ed\\b', str1))
print(re.findall('\Bed\\b', str1))
print(re.findall(r'\Bed\b', str1))
print(re.findall(r'ed\b', str1))

 

原创视频,版权所有,未经允许,切勿转载,违者必究!
回顶部