1
This commit is contained in:
@@ -2784,32 +2784,32 @@ public class JDUtil {
|
||||
Matcher matcher = urlPattern.matcher(text);
|
||||
return matcher.find() ? matcher.group(0) : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 从字符串中解析出价格数值
|
||||
* @param input 包含价格的字符串(如"🔥7.2折⎜◉854.64💰")
|
||||
* @return 解析出的价格(double类型),解析失败返回null
|
||||
* 从字符串中解析价格(基于◉和💰符号定位)
|
||||
* @param input 包含价格的字符串
|
||||
* @return 解析出的价格,失败返回null
|
||||
*/
|
||||
public static Double parsePrice(String input) {
|
||||
if (input == null || input.isEmpty()) {
|
||||
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);
|
||||
|
||||
// 从右向左查找最后一个数字(价格通常在字符串末尾)
|
||||
double price = -1;
|
||||
while (matcher.find()) {
|
||||
String numberStr = matcher.group();
|
||||
if (matcher.find()) {
|
||||
try {
|
||||
price = Double.parseDouble(numberStr);
|
||||
// 提取第一个捕获组(括号内的数字部分)
|
||||
return Double.parseDouble(matcher.group(1));
|
||||
} catch (NumberFormatException e) {
|
||||
// 忽略转换失败的情况
|
||||
continue;
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
return price >= 0 ? price : null;
|
||||
// 如果未找到匹配,返回null
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user