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

如何用Python提取字符串中Title参数后的所有对应值?

Python提取字符串中Title属性的对应值

你猜的没错,用Python的re库做正则匹配是最高效的方案,直接用re.findall()就能一次性提取所有符合格式的值。

基础实现代码

import re

example = 'jnkfksieufsieufsejfuies Title="Something" daowijdaowdnobngfoinbijfgh Title="something else"'
# 匹配所有Title="xxx"格式的内容,提取双引号内的部分
matched_values = re.findall(r'Title="(.*?)"', example)
print(matched_values)
# 输出结果: ['Something', 'something else']

正则表达式说明

  • r'Title="(.*?)"':用原始字符串格式避免转义问题;
  • (.*?):非贪婪捕获组,只会匹配到下一个双引号为止,确保每个Title对应的值被单独提取;
  • re.findall():自动返回所有捕获到的内容组成的列表。

兼容更多场景的版本

如果遇到Title和=之间有空格、或者用单引号包裹值的情况,可以调整正则表达式兼容这些场景:

import re

example = 'jnkfksieufsieufsejfuies Title = "Something" daowijdaowdnobngfoinbijfgh Title=\'something else\''
matched_values = re.findall(r'Title\s*=\s*["\'](.*?)["\']', example)
print(matched_values)
# 输出结果: ['Something', 'something else']

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.10 22:40:24