在Python中从字符串中删除子字符串?
我目前面临的问题是,我有一个字符串(deeplink),我想从中提取一个子字符串:在Python中从字符串中删除子字符串?,python,string,numbers,web-crawler,Python,String,Numbers,Web Crawler,我目前面临的问题是,我有一个字符串(deeplink),我想从中提取一个子字符串: <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
<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
?
我试过使用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]
您可以使用
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]
您可以使用
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)
您可以使用
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']