[英]Removing substrings from a string in Python?
I´m currently facing the problem that I have a string (deeplink) of which I want to extract one certain substring: 我目前面临的问题是我有一个字符串(深层链接),我想提取一个特定的子字符串:
<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>
I expect to get extract the following information from the strings above: 我希望从上面的字符串中提取以下信息:
t107728
t65554
How can I only extract for example the substring t107728
from the the first string above? 例如,如何仅从上面的第一个字符串中提取子字符串
t107728
? I tried it with the split and sub function but it does not work out 我尝试使用split和sub函数,但无法正常工作
Can you guys help me out? 你们能帮我吗? Any feedback is appreciated
任何反馈表示赞赏
You can use re
: 您可以使用
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]
Output: 输出:
['t107728', 't65554']
You can try this one, using the split
function: 您可以使用
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)
Output: 输出:
['t107728', 't65554']
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.