You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在Microsoft Excel中提取URL倒数第二个/之后的内容

批量提取URL中倒数第二个/之后的内容

下面提供几种实用的解决方案,适用于不同场景:

Python 脚本实现

先清理URL末尾的斜杠,再按斜杠分割路径,取倒数第二个元素即可:

urls = [
    "https://www.example.com/folder1/folder2/folder3/path-name-here/12345678/",
    "https://www.example.com/folder1/folder2/path-name-here/39089/"
]

for url in urls:
    cleaned_url = url.rstrip('/')  # 移除末尾多余的斜杠
    path_parts = cleaned_url.split('/')
    print(path_parts[-2])

正则表达式通用方案

适合支持正则的文本编辑器(如Notepad++、Sublime Text)或多数编程语言,正则匹配倒数第二个斜杠后的内容:
正则表达式:/([^/]+)/[^/]*$
提取捕获组1的内容即可。

以Python为例:

import re

pattern = r'/([^/]+)/[^/]*$'
urls = [
    "https://www.example.com/folder1/folder2/folder3/path-name-here/12345678/",
    "https://www.example.com/folder1/folder2/path-name-here/39089/"
]

for url in urls:
    match_result = re.search(pattern, url)
    if match_result:
        print(match_result.group(1))

命令行 awk 处理文本文件

如果你的URL保存在文本文件(如urls.txt,每行一个URL),可以用awk快速提取:

awk -F '/' '{print $(NF-1)}' urls.txt

原理:以斜杠为分隔符,NF是当前行的字段总数,$(NF-1)就是倒数第二个字段,正好对应目标内容。

内容的提问来源于stack exchange,提问作者natan specialist

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.04 09:17:36