贪婪模式与非贪婪模式

#!/usr/bin/python3
# -*- coding: utf-8 -*-


import re


str = "abcdcd"


# re.findall()
# 写法方式二
# 1. 编译生成正则表达式对象
# (.*) 贪婪匹配 尽可能多的匹配内容
# pattern = re.compile("a(.*)d")


# (.*?) 非贪婪匹配 匹配到就结束
pattern = re.compile("a(.*?)d")


# 2. 通过表达式对象进行正则匹配
result = pattern.findall(str)


print(result)

猜你喜欢

转载自blog.csdn.net/qq_41868948/article/details/81012165
今日推荐