目前我有如下字符串:
urls = [
'<a href=https://energyplus.net/weather-download/asia_wmo_region_2/IND//IND_Kota.424520_ISHRAE/IND_Kota.424520_ISHRAE.epw>Download Weather File</a>',
'<a href=https://energyplus.net/weather-download/europe_wmo_region_6/ESP//ESP_Alicante.083600_SWEC/ESP_Alicante.083600_SWEC.epw>Download Weather File</a>'
]
以及如下所示的正则表达式搜索:
for url in urls:
match = re.search(r'href=[\'"]?([^\'" >]+)', url)
if match:
url = match.group(1)
url
返回:
https://energyplus.net/weather-download/asia_wmo_region_2/IND//IND_Kota.424520_ISHRAE/IND_Kota.424520_ISHRAE.epw
https://energyplus.net/weather-download/europe_wmo_region_6/ESP//ESP_Alicante.083600_SWEC/ESP_Alicante.083600_SWEC.epw
我希望过滤掉包含单词SWEC
的url,这样第二个url字符串就不匹配了。我认为这可能与(?!SWEC)
有关,但即使这是正确的,我也不确定如何将其合并到当前的正则表达式搜索中
如果能得到建议,我将不胜感激
这里有一个使用"The Greatest Regex Trick Ever"的解决方案
诀窍是先匹配你不想要的,然后捕获你想要的。这仍然会将URL与SWEC匹配,但捕获组将为空,因此您需要调整代码以处理此问题
您可以将
.*
添加到负前瞻(?!.*SWEC)
,这样正则表达式将断言字符序列不匹配其后面紧跟单词SWEC
的任何字符(换行符除外)。这种消极的前瞻不需要进入您的正则表达式捕获组,但它有助于减少查找有效匹配的步骤数Regex101example
这里可能不需要正则表达式。例如
试试看:
甚至:
相关问题 更多 >
编程相关推荐