为了账号安全,请及时绑定邮箱和手机立即绑定

正则表达式搜索特定的文本结构

正则表达式搜索特定的文本结构

ITMISS 2021-10-26 18:11:30
我想在字符串中查找某个结构的所有结果,最好使用正则表达式。要查找所有网址,可以使用re.findall('https?://(?:[-\w.]|(?:%[\da-fA-F]{2}))+', decode)它返回 'https://en.wikipedia.org'我想要一个正则表达式字符串,它可以找到:href="/wiki/*anything*"
查看完整描述

1 回答

?
尚方宝剑之说

TA贡献1788条经验 获得超4个赞

OP:开头必须是 href="/wiki/ 中间可以是任何内容,结尾必须是 "


st = "since-OP-did-not-provide-a-sample-string-34278234$'blahhh-okay-enough.href='/wiki/anything/everything/nothing'okay-bye"    

print(st[st.find('href'):st.rfind("'")+1])

输出:


href='/wiki/anything/everything/nothing'

编辑:


如果我们要解析可能的 html,我会选择 BeautifulSoup 。


from bs4 import BeautifulSoup


text = '''<a href='/wiki/anything/everything/nothing'><img src="/hp_imgjhg/411/1/f_1hj11_100u.jpg" alt="dyufg" />well wait now <a href='/wiki/hello/how-about-now/nothing'>'''

soup = BeautifulSoup(text, features="lxml")


for line in soup.find_all('a'):

    print("href =",line.attrs['href'])

输出:


href = /wiki/anything/everything/nothing

href = /wiki/hello/how-about-now/nothing


查看完整回答
反对 回复 2021-10-26
  • 1 回答
  • 0 关注
  • 225 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

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

帮助反馈 APP下载

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

公众号

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