如何在报价之间输入正则表达式?

时间:2017-04-09 10:12:22

标签: python regex

我需要在这个示例中获取引号之间的条目:正则表达式href="x....dkjads...href="y"并返回xy

[<a class="lightbox" href="fileadmin/user_upload/images/Sprachen/Englisch/USA/San_Diego/San_Diego_EC/EC_San_Diego_Galerie.jpg" title=""><img alt="Sprachschule EC San Diego" border="0" height="80" src="typo3temp/pics/EC_San_Diego_Galerie_d1def1bf4a.jpg" title="Sprachschule EC San Diego (Copyright EC San Diego. All rights reserved.)" width="80"/></a>, <a class="lightbox" href="fileadmin/user_upload/images/Sprachen/Englisch/USA/San_Diego/San_Diego_EC/EC_San_Diego_Galerie-_1_.jpg" title=""><img alt="Sprachschule EC San Diego 2" border="0" height="80" src="typo3temp/pics/EC_San_Diego_Galerie-_1__fd87630014.jpg" title="Sprachschule EC San Diego 2 (Copyright EC San Diego. All rights reserved.)" width="80"/></a>, <a class="lightbox" href="fileadmin/user_upload/images/Sprachen/Englisch/USA/San_Diego/San_Diego_EC/EC_San_Diego_Galerie-_10_.jpg" title=""><img alt="Sprachschule EC San Diego 3" border="0" height="80" src="typo3temp/pics/EC_San_Diego_Galerie-_10__a8ed60c277.jpg" title="Sprachschule EC San Diego 3 (Copyright EC San Diego. All rights reserved.)"

如何在正则表达式中输入以在开头搜索多个完全字符?

这一个(?<=\").*?(?=\")会返回" "之间的所有内容,而(?<=\{href="}).*?(?=\")之类的内容不起作用

1 个答案:

答案 0 :(得分:1)

如果您想匹配<content>中的href="<content>",要匹配的模式为href=\"(.*?)\"regex101 demo)。

使用python re模块,您可以执行以下操作:

>>> a= """
... [<a class="lightbox" href="fileadmin/user_upload/images/Sprachen/Englisch/USA/San_Diego/San_Diego_EC/EC_San_Diego_Galerie.jpg" title=""><img alt="Sprachschule EC San Diego" border="0" height="80" src="typo3temp/pics/EC_San_Diego_Galerie_d1def1bf4a.jpg" title="Sprachschule EC San Diego (Copyright EC San Diego. All rights reserved.)" width="80"/></a>, <a class="lightbox" href="fileadmin/user_upload/images/Sprachen/Englisch/USA/San_Diego/San_Diego_EC/EC_San_Diego_Galerie-_1_.jpg" title=""><img alt="Sprachschule EC San Diego 2" border="0" height="80" src="typo3temp/pics/EC_San_Diego_Galerie-_1__fd87630014.jpg" title="Sprachschule EC San Diego 2 (Copyright EC San Diego. All rights reserved.)" width="80"/></a>, <a class="lightbox" href="fileadmin/user_upload/images/Sprachen/Englisch/USA/San_Diego/San_Diego_EC/EC_San_Diego_Galerie-_10_.jpg" title=""><img alt="Sprachschule EC San Diego 3" border="0" height="80" src="typo3temp/pics/EC_San_Diego_Galerie-_10__a8ed60c277.jpg" title="Sprachschule EC San Diego 3 (Copyright EC San Diego. All rights reserved.)"
... 
... """
>>> import re
>>> re.findall(r'href=\"(.*?)\"',a)
['fileadmin/user_upload/images/Sprachen/Englisch/USA/San_Diego/San_Diego_EC/EC_San_Diego_Galerie.jpg', 'fileadmin/user_upload/images/Sprachen/Englisch/USA/San_Diego/San_Diego_EC/EC_San_Diego_Galerie-_1_.jpg', 'fileadmin/user_upload/images/Sprachen/Englisch/USA/San_Diego/San_Diego_EC/EC_San_Diego_Galerie-_10_.jpg']
>>> 

希望这有帮助。