网站制作学习网Python→正文:python 函数正则获取tag 内容
字体:

python 函数正则获取tag 内容

Python 2024/3/22 21:01:12  点击:不统计

学习www.网for站asp制.cn作
 python 通过 正则获取tag内容,直接看下面函数

 
参数为tag 为 标签只
string 为字符串
strip 布尔 为是否去除前后空格 


import re


def extract_between_tags(tag: str, string: str, strip: bool = False) -> list[str]:
ext_list = re.findall(f"<{tag}>(.+?)</{tag}>", string, re.DOTALL)
if strip:
ext_list = [e.strip() for e in ext_list]
return ext_list


function_calling_message = "asdf<tr>this is forasp.cn</tr>"
match_data = extract_between_tags("tr", function_calling_message, True)
if len(match_data) > 0:
print(match_data[0]) else: print("no match")

·上一篇:python代码获取扩展版本 >>    ·下一篇:conda 常用命令 >>
推荐文章
最新文章