python之使用正则表达式获取两边特定的字符串

导入re包
import re
比如获取网页里面
https://baidu.com/b1/哈哈哈哈.html
在b1/后面并且在.html前面的对象
可以用正则表达式

import re

url = 'https://baidu.com/b1/哈哈哈哈.html'

pattern = '(https.*?/b1/)|(.html)'

result = re.sub(pattern, '', url)

print(result)

猜你喜欢

转载自blog.csdn.net/qq_42794826/article/details/108891222