我目前面临的问题是,我有一个字符串(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-zum-fuji-ab-tokio-t65554/?partner_id=M1</deeplink>我希望从上面的字符串中提取以下信息:
t107728
t65554例如,我如何才能从上面的第一个字符串中提取出子字符串t107728呢?我用分裂和sub函数试过了,但不起作用
你们能帮帮我吗?如有任何反馈,我们将不胜感激
发布于 2018-04-04 03:30:07
您可以使用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']发布于 2018-04-04 20:28:37
您可以使用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']https://stackoverflow.com/questions/49637751
复制相似问题