运用边界匹配练习
代码演示:
#匹配以数字开头的单词 s = 'hello world 1st, 2st' #\b代表单词开始或者结束,\d代表数字,\w代表有效字符 result = re.findall(r'\b\d\w*', s) print(result) #匹配s结尾单词 s = 'this is test' #s\b:代表s结尾单词 result = re.findall(r'\w*s\b', s) print(result) s = 'hello world, this is test' #查找单词中包含o的单词 result = re.findall(r'\w*o\w*', s) print(result) #查找在单词位置的o result = re.findall(r'\Bo\B', s) print(result)
结果:
['1st', '2st'] ['this', 'is'] ['hello', 'world'] ['o']
上一篇:python3中re模块有什么用
下一篇:python中多进程指的是什么