python - string function in Python to extract between two characters
问题描述
I have the below string and I want to extract everything from <img...
to the closing "
after .jpg
.
I tried the below, but it doesn't find just the first "
but rather the very end.
Can anyone help?
In [14]: start = 'img src="'
In [15]: end = '"'
print string[string.find(start)+len(start):string.rfind(end)]
STRING:
<p><a href="https://news.yahoo.com/us-ambassador-takes-post-united-nations-141833297.html"><img src="http://l1.yimg.com/uu/api/res/1.2/1f8jyGM.NfkxLb_.OgMaIQ--/YXBwaWQ9eXRhY2h5b247aD04Njt3PTEzMDs-/http://media.zenfs.com/en_us/News/afp.com/f5bbc19135065fcfff40e6ece9650f4ab225fa97.jpg" width="130" height="86" alt="New US ambassador takes up post at United Nations" align="left" title="New US ambassador takes up post at United Nations" border="0" ></a>US Ambassador Kelly Craft took up her post at the United Nations on Thursday, vowing to defend America's values and interests nine months after the departure of her high-profile predecessor Nikki Haley. Craft, 57, served previously as US ambassador to Canada where she was involved in negotiations on a new US Mexico Canada free trade agreement.<p><br clear="all">
解决方案
你可以像这样使用正则表达式,如果你确定它总是一样的。
<img.*?jpg\"
这是它的链接,Regex101 您可以根据需要进行调整。Regex 是它的正确工具,而不是 sting find 和 len 等等。
推荐阅读
- powershell - 如何使用 powershell 删除所有 google chrome cookie
- javascript - 在网络上启用 Google 帐户选择器
- ios - 我可以使用 ARKit 2 测量眼睛吗
- sql - 查找最新修改的数据库,它可以是在 SQL 服务器中修改的存储过程、表、函数、视图、索引
- php - Laravel 会话未收集填充
- swift - RxSwift - 映射后函数的调用序列
- javascript - Highcharts 无法在生产服务器上正确呈现
- excel - 用于从另一个表中对新表中的数据进行排名的公式
- javascript - jQuery - 在 CSS 背景颜色切换上放置 1 秒过渡
- angularjs - 开始 AngularJS、NodeJS、Express 和 Firebase?