首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >正则表达式来提取没有不想要的单词的URL

正则表达式来提取没有不想要的单词的URL
EN

Stack Overflow用户
提问于 2021-07-24 01:18:31
回答 3查看 155关注 0票数 1

目前,我有这样的字符串:

代码语言:javascript
复制
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>'
]

一个正则表达式搜索如下:

代码语言:javascript
复制
for url in urls:
    match = re.search(r'href=[\'"]?([^\'" >]+)', url)
    if match:
        url = match.group(1)

url返回:

代码语言:javascript
复制
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)有关,但即使这是正确的,我也不知道如何将其合并到当前的正则表达式搜索中。

如果你能给我建议的话,我很感激。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2021-07-24 13:52:17

这里有一个使用“史上最伟大的Regex诡计”的解决方案

代码语言:javascript
复制
for url in urls:
    match = re.search(r'href=[\'"]?(?:[^\'" >]*SWEC[^\'" >]*|([^\'" >]+))', url)
    if match and len(match.group(1)) > 0:
        url = match.group(1)

诀窍是先匹配你不想要的,然后捕捉你想要的。这仍然会与SWEC匹配urls,但是捕获组将是空的,因此您需要调整代码来处理这个问题。

票数 0
EN

Stack Overflow用户

发布于 2021-07-24 01:51:17

这里可能不需要Regex。例如..。

Try:

代码语言:javascript
复制
# list of urls
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>'
]

# check length of list (2)
print(len(urls))

# loop through the list
for i, url in enumerate(urls):
#for url in urls: #if you remove the printing you can revert to this and delete the above enumerate line
    #check if the substring 'SWEC' is in the current element of the list
    if 'SWEC' in url:
        #if so delete that element
        urls.remove(url)
        #print a message to say it's been deleted
        print('Found.  Removing item ' + str(i))

# recheck the length of the list (1)
print(len(urls))

或偶数:

代码语言:javascript
复制
urls = [x for x in urls if 'SWEC' not in x]
票数 0
EN

Stack Overflow用户

发布于 2021-07-24 14:01:01

您可以将.*添加到负的(?!.*SWEC)中,这样正则表达式将断言字符序列不匹配其后面跟着单词SWEC的任何字符(换行符除外)。这种负面展望不需要进入regex捕获组,但它有助于减少查找有效匹配的步骤数。

代码语言:javascript
复制
import re

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=[\'"]?((?!.*SWEC)[^\'" >]+)', url)
    if match:
        url = match.group(1)
        print(url)

# https://energyplus.net/weather-download/asia_wmo_region_2/IND//IND_Kota.424520_ISHRAE/IND_Kota.424520_ISHRAE.epw

Regex101 示例

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68506298

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档