(1)问题分析
面试官主要考核应聘者对正则表达式的了解程度
(2)核心问题讲解
在形式上非贪婪模式有一个“?”作为该部分的结束标志。
在功能上贪婪模式是尽可能多的匹配当前正则表达式,可能会包含好几个满足正则表达式的字符串,非贪婪模式,在满足所有正则表达式的情况下尽可能少的匹配当前正则表达式。
(3)问题扩展
import re
example = "
goodsname"
# 贪婪模式
greed_pattern = re.compile("
.*")
# 非贪婪模式
not_greed_pattern = re.compile("
.*?")
greed_result = greed_pattern.search(example)
not_greed_result = not_greed_pattern.search(example)
print(f"贪婪模式:{greed_result.group()}")
print(f"非贪婪模式:{not_greed_result.group()}")
(4)结合项目中使用
无
猜你喜欢:
complie()函数预编译正则表达式
Python正则表达式re模块常用函数有哪些?
使用Django中间件的六种方法[python培训]
大阳城app官网入口python+数据分析课程