This commit is contained in:
雷欧(林平凡)
2025-08-08 11:30:40 +08:00
parent 4708323384
commit 60f8c51f44

View File

@@ -2784,32 +2784,32 @@ public class JDUtil {
Matcher matcher = urlPattern.matcher(text); Matcher matcher = urlPattern.matcher(text);
return matcher.find() ? matcher.group(0) : null; return matcher.find() ? matcher.group(0) : null;
} }
/** /**
* 从字符串中解析价格数值 * 从字符串中解析价格(基于◉和💰符号定位)
* @param input 包含价格的字符串(如"🔥7.2折⎜◉854.64💰" * @param input 包含价格的字符串
* @return 解析出的价格double类型解析失败返回null * @return 解析出的价格失败返回null
*/ */
public static Double parsePrice(String input) { public static Double parsePrice(String input) {
if (input == null || input.isEmpty()) { if (input == null || input.isEmpty()) {
return null; return null;
} }
// 正则表达式:匹配一个或多个数字,可包含一个小数点 // 匹配◉和💰之间的数字(包含小数点
Pattern pattern = Pattern.compile("\\d+\\.\\d+|\\d+"); // \u25C9 是◉符号,\uD83D\uDCB0 是💰符号
Pattern pattern = Pattern.compile("\\u25C9(\\d+\\.\\d+|\\d+)\\uD83D\\uDCB0");
Matcher matcher = pattern.matcher(input); Matcher matcher = pattern.matcher(input);
// 从右向左查找最后一个数字(价格通常在字符串末尾) if (matcher.find()) {
double price = -1;
while (matcher.find()) {
String numberStr = matcher.group();
try { try {
price = Double.parseDouble(numberStr); // 提取第一个捕获组(括号内的数字部分)
return Double.parseDouble(matcher.group(1));
} catch (NumberFormatException e) { } catch (NumberFormatException e) {
// 忽略转换失败的情况 return null;
continue;
} }
} }
return price >= 0 ? price : null; // 如果未找到匹配,返回null
return null;
} }
} }