繁体   English   中英

从Python中的字符串中删除子字符串?

[英]Removing substrings from a string in Python?

我目前面临的问题是我有一个字符串(深层链接),我想提取一个特定的子字符串:

   <deeplink>https://www.jsox.de/tokyo-l200/tokio-skytree-ticket-fuer-einlass-ohne-anstehen-t107728/?partner_id=M1</deeplink>

   <deeplink>https://www.jsox.de/tokyo-l201/ganztaegige-bustour-zum-fuji-ab-tokio-t65554/?partner_id=M1</deeplink>

我希望从上面的字符串中提取以下信息:

t107728
t65554

例如,如何仅从上面的第一个字符串中提取子字符串t107728 我尝试使用split和sub函数,但无法正常工作

你们能帮我吗? 任何反馈表示赞赏

您可以使用re

import re
s = ['<deeplink>https://www.jsox.de/tokyo-l200/tokio-skytree-ticket-fuer-einlass-ohne-anstehen-t107728/?partner_id=M1</deeplink>', '<deeplink>https://www.jsox.de/tokyo-l201/ganztaegige-bustour-zum-fuji-ab-tokio-t65554/?partner_id=M1</deeplink>']
new_s = [re.findall('[a-zA-Z0-9]+(?=/\?)', i)[0] for i in s]

输出:

['t107728', 't65554']

您可以使用split函数尝试这一操作:

strings = ["<deeplink>https://www.jsox.de/tokyo-l200/tokio-skytree-ticket-fuer-einlass-ohne-anstehen-t107728/?partner_id=M1</deeplink>", "<deeplink>https://www.jsox.de/tokyo-l201/ganztaegige-bustour-zum-fuji-ab-tokio-t65554/?partner_id=M1</deeplink>"]

results = [elem.split("/?")[0].split("-")[-1] for elem in strings]

print(results)

输出:

['t107728', 't65554']

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM