python中关于正则提取的小例子 发表于 2019-10-30 | 分类于 python | 评论数: | 阅读次数: 本文字数: 943 | 阅读时长 ≈ 0:01 一个小例子,关于正则表达式的应用。python中正则例子。 直接代码直接上代码吧。不多说,应用中用到了。 1234567891011121314151617181920212223import re # url = "http://news.nankai.edu.cn/ywsd/index${12-34}.shtml" url = "http://news.nankai.edu.cn/ywsd/index.shtml" matchObj = re.match(r'(.*)\$\{(.*)-(.*)\}(.*)', url, re.M | re.I) if matchObj: print("matchObj.group() : ", matchObj.group()) print("matchObj.group(1) : ", matchObj.group(1)) print("matchObj.group(2) : ", matchObj.group(2)) print("matchObj.group(3) : ", matchObj.group(3)) print("matchObj.group(4) : ", matchObj.group(4)) for i in range(int(matchObj.group(2)),int(matchObj.group(3))+1): urlnn=matchObj.group(1)+str(i)+matchObj.group(4) print(urlnn) else: print(url) pass 饮水思源 https://www.runoob.com/python/python-reg-expressions.html 本文作者: WQian 本文链接: https://wqian.net/blog/2019/1030-python-re-index.html 版权声明: 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!