1
This commit is contained in:
@@ -2786,7 +2786,7 @@ public class JDUtil {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 从字符串中解析价格(基于◉和💰符号定位)
|
* 从字符串中解析价格(优化版,适配更多符号情况)
|
||||||
* @param input 包含价格的字符串
|
* @param input 包含价格的字符串
|
||||||
* @return 解析出的价格,失败返回null
|
* @return 解析出的价格,失败返回null
|
||||||
*/
|
*/
|
||||||
@@ -2795,21 +2795,37 @@ public class JDUtil {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 匹配◉和💰之间的数字(包含小数点)
|
// 优化正则:匹配◉后任意数字(含小数点),直到遇到💰或字符串结束
|
||||||
// \u25C9 是◉符号,\uD83D\uDCB0 是💰符号
|
// 增加对可能的符号变体的兼容
|
||||||
Pattern pattern = Pattern.compile("\\u25C9(\\d+\\.\\d+|\\d+)\\uD83D\\uDCB0");
|
Pattern pattern = Pattern.compile("\\u25C9([\\d.]+)\\s*\\uD83D\\uDCB0?");
|
||||||
Matcher matcher = pattern.matcher(input);
|
Matcher matcher = pattern.matcher(input);
|
||||||
|
|
||||||
if (matcher.find()) {
|
if (matcher.find()) {
|
||||||
try {
|
try {
|
||||||
// 提取第一个捕获组(括号内的数字部分)
|
String priceStr = matcher.group(1).trim();
|
||||||
return Double.parseDouble(matcher.group(1));
|
// 确保是有效的数字格式(至少一个数字,最多一个小数点)
|
||||||
|
if (priceStr.matches("\\d+\\.?\\d*|\\d*\\.\\d+")) {
|
||||||
|
return Double.parseDouble(priceStr);
|
||||||
|
}
|
||||||
|
} catch (NumberFormatException e) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 二次尝试:如果找不到完整符号对,尝试提取◉后面的第一个数字
|
||||||
|
Pattern fallbackPattern = Pattern.compile("\\u25C9([\\d.]+)");
|
||||||
|
Matcher fallbackMatcher = fallbackPattern.matcher(input);
|
||||||
|
if (fallbackMatcher.find()) {
|
||||||
|
try {
|
||||||
|
String priceStr = fallbackMatcher.group(1).trim();
|
||||||
|
if (priceStr.matches("\\d+\\.?\\d*|\\d*\\.\\d+")) {
|
||||||
|
return Double.parseDouble(priceStr);
|
||||||
|
}
|
||||||
} catch (NumberFormatException e) {
|
} catch (NumberFormatException e) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 如果未找到匹配,返回null
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user