2024-07-22 13:45:14 +00:00
|
|
|
|
import re
|
2024-07-22 07:21:36 +00:00
|
|
|
|
|
2024-07-22 13:45:14 +00:00
|
|
|
|
|
|
|
|
|
def parse_online_time(online_time_str):
|
|
|
|
|
# 如果字符串中同时包含小时和分钟
|
|
|
|
|
if "小时" in online_time_str and "分钟" in online_time_str:
|
|
|
|
|
match = re.search(r'(\d+)小时(\d+)分钟', online_time_str)
|
|
|
|
|
if match:
|
|
|
|
|
hours, minutes = match.groups()
|
|
|
|
|
return int(hours) * 60 + int(minutes)
|
|
|
|
|
|
|
|
|
|
# 如果字符串中只包含小时
|
|
|
|
|
elif "小时" in online_time_str:
|
|
|
|
|
match = re.search(r'(\d+)小时', online_time_str)
|
|
|
|
|
if match:
|
|
|
|
|
hours = match.group(1)
|
|
|
|
|
return int(hours) * 60
|
|
|
|
|
|
|
|
|
|
# 如果字符串中只包含分钟
|
|
|
|
|
elif "分钟" in online_time_str:
|
|
|
|
|
match = re.search(r'(\d+)分钟', online_time_str)
|
|
|
|
|
if match:
|
|
|
|
|
minutes = match.group(1)
|
|
|
|
|
return int(minutes)
|
|
|
|
|
|
|
|
|
|
# 如果字符串中既不包含小时也不包含分钟,则返回0
|
|
|
|
|
else:
|
|
|
|
|
return 0
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# 测试函数
|
|
|
|
|
print(parse_online_time("晚高峰 目标在线时长:2小时30分钟,目标完单量:5")) # 应该返回150
|
|
|
|
|
print(parse_online_time("晚高峰 目标在线时长:1小时目标完单量:5")) # 应该返回60
|
|
|
|
|
print(parse_online_time("晚高峰 目标在线时长:30分钟,目标完单量:5")) # 应该返回30
|
|
|
|
|
print(parse_online_time("晚高峰 目标完单量:5")) # 应该返回0
|