import re
url = "https://www.baidu.com/company/13828?param=abc"
com_id = re.match(".*company/(\d+)", url)
print com_id.group(1)
将其封装为函数
import re
def get_digit(url, reg_exp=".*/(\d+)"):
"""匹配网址中的数字
:param
url{str}: 网址字符串
reg_exp{str}: 正则匹配规则
:return
digit {None}/{str}: 空或者数字字符串
"""
digit = None
pattern = re.compile(reg_exp)
result = pattern.match(url)
if result:
digit = result.group(1)
return digit
if __name__ == '__main__':
url = ""
ret = get_digit(url)
print(ret)
url = "https://www.baidu.com/company/13828?param=abc"
ret = get_digit(url)
print(ret)
url = "https://www.baidu.com/company/13828?param=234234"
ret = get_digit(url)
print(ret)