需求
url 中有一个id信息,需要从url中匹配出来
匹配示例如下
# -*- coding: utf-8 -*- import re url = 'https://www.demo.com/goods/item/id/12562.html' ret = re.match('.*/goods/item/id/(\d+)\.html', url) print(ret.group(1)) # 12562
代码优化
我们单独给这个功能写一个函数,以便代码复用,不仅代码复用,同时也能增强代码美观
# -*- coding: utf-8 -*- import re def get_url_id(url): ret = re.match('.*/goods/item/id/(\d+)\.html', url) if ret: return ret.group(1) if __name__ == '__main__': url = 'https://www.baidu.com/goods/item/id/12562.html' url_id = get_url_id(url) print(url_id) # 12562
命名组
使用正则命名组,能更优雅的处理匹配结果
# -*- coding: utf-8 -*- import re def get_url_id(url): ret = re.match('.*/goods/item/id/(?P<uid>\d+)\.html', url) if ret: # {'uid': '12562'} return ret.groupdict().get('uid') if __name__ == '__main__': url = 'https://www.demo.com/goods/item/id/12562.html' print(get_url_id(url)) # 12562