请稍等 ...
×

采纳答案成功!

向帮助你的同学说点啥吧!感谢那些助人为乐的人

import re s = 'life is short, I use Python, I love Python'

import re
s = 'life is short, I use Python, I love Python’
print(re.findall("(life.* (Python)?)",s))

输出是:[(‘life is short, I use Python, I love Python’, ‘Python’)] 为什么会去到第二个Python

正在回答 回答被采纳积分+3

插入代码

1回答

GZK199511 2018-10-16 16:55:39
1
2
3
4
import re
= 'life is short, I use Python, I love Python'
= re.findall("Python.*?Python",s)
print(r)

这样是正确的,你的(python)?Python识别的意思是识别到0或者1个‘python’,而不是防止贪婪的意思

0 回复 有任何疑惑可以回复我~
  • 提问者 ldc123_ #1
    life is short, I use Python  只获取这段话该怎么写?
    回复 有任何疑惑可以回复我~ 2018-10-16 19:43:38
  • 一个酸溜溜的小西瓜 回复 提问者 ldc123_ #2
    import re
    s = 'life is short, I use Python, I love Python'
    print(re.findall('life(.*)Python',s)
    回复 有任何疑惑可以回复我~ 2018-10-19 14:57:21
  • 我这个不对,只是打印life与python之间的
    回复 有任何疑惑可以回复我~ 2018-10-19 15:29:02
问题已解决,确定采纳
还有疑问,暂不采纳
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号