gpt4 book ai didi

python - 如何使用正则表达式搜索和删除字符串?

转载 作者:行者123 更新时间:2023-11-28 20:54:57 25 4
gpt4 key购买 nike

我有这样一个字符串:

'<div class="hotels-hotel-review-about-with-photos-Reviews__subratingRow--2u0CJ"><span class="ui_bubble_rating bubble_45"></span><div class="hotels-hotel-review-about-with-photos-Reviews__subratingLabel--H8ZI0">Location</div></div>'

我想提取 bubble_ 之后的数值,即 45

我试过:

rating = re.search('bubble_(\d+)', str(line)).group(0)
rating = re.sub("bubble_" , '', rating)

我的代码有效,但我想知道是否有更多的 pythonic 可以做到这一点。 (而不是两行代码,只有一行!)谢谢

最佳答案

使用这个正则表达式:

(?<=bubble_)(\d+)

并使用一行:

rating = re.search('(?<=bubble_)(\d+)', str(line)).group(0)

关于python - 如何使用正则表达式搜索和删除字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58142679/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com