价格的正则表达式 - Android
Regular expression for price - Android
我有一个 string
如下所示:
dfdfm;lg 2500$ jshfsnefsfz5405€mnvkjdf64rfmkd554668¢ odsfrknegj 885486¥ dsflkef 588525dollar
我得到的值低于这个 [\d,]+\s*\$|[\d,]+\s*€|[\d,]+\s*¥|[\d,]+\s*¢|[\d,]+\s*dollar
:
2500$
5405€
554668¢
885486¥
588525dollar
问题:但我不需要这些$ € ¢ ¥ dollar
。我如何在顶级正则表达式中删除这些?
这是我的方法:
private String getPrice(String caption) {
String pricePattern = "[\d,]+\s*\$|[\d,]+\s*€|[\d,]+\s*¥|[\d,]+\s*¢|[\d,]+\s*dollar|[\d,]+\s*Euro";
List<String> lstPrice = new ArrayList<>();
Pattern rPrice = Pattern.compile(pricePattern);
Matcher mPrice = rPrice.matcher(caption);
while (mPrice.find()) {
lstPrice.add(mPrice.group());
}
if (lstPrice.size() > 0) {
return lstPrice.get(0);
}
return "";
}
你可以试试 replaceAll
Replaces every subsequence of the input sequence that matches the
pattern with the given replacement string.
String pricePattern="2500$ 5405€ 554668¢ 885486¥ 588525dollar";
pricePattern= pricePattern.replaceAll("[^\d+]", " "); //2500 5405 554668 885486 588525
如果您需要 return 所有价格,请确保您的 getPrice
方法 returns List<String>
并调整正则表达式以匹配价格但仅捕获数字:
private List<String> getPrice(String caption) {
String pricePattern = "(?i)(\d[\d,]*)\s*(?:[$€¥¢]|dollar|Euro)";
List<String> lstPrice = new ArrayList<>();
Pattern rPrice = Pattern.compile(pricePattern);
Matcher mPrice = rPrice.matcher(caption);
while (mPrice.find()) {
lstPrice.add(mPrice.group(1));
}
return lstPrice;
}
String s = "dfdfm;lg 2500$ jshfsnefsfz5405€mnvkjdf64rfmkd554668¢ odsfrknegj 885486¥ dsflkef 588525dollar";
System.out.println(getPrice(s));
returns
[2500, 5405, 554668, 885486, 588525]
图案详情:
(?i)
- 不区分大小写的修饰符(嵌入标志选项)
(\d[\d,]*)
- 第 1 组捕获一个数字,然后捕获 0+ 个数字或 ,
\s*
- 0+ 个空格
(?:[$€¥¢]|dollar|Euro)
- $
、€
、¥
、¢
、dollar
或 euro
(不区分大小写通过 (?i)
) 启用搜索
我有一个 string
如下所示:
dfdfm;lg 2500$ jshfsnefsfz5405€mnvkjdf64rfmkd554668¢ odsfrknegj 885486¥ dsflkef 588525dollar
我得到的值低于这个 [\d,]+\s*\$|[\d,]+\s*€|[\d,]+\s*¥|[\d,]+\s*¢|[\d,]+\s*dollar
:
2500$ 5405€ 554668¢ 885486¥ 588525dollar
问题:但我不需要这些$ € ¢ ¥ dollar
。我如何在顶级正则表达式中删除这些?
这是我的方法:
private String getPrice(String caption) {
String pricePattern = "[\d,]+\s*\$|[\d,]+\s*€|[\d,]+\s*¥|[\d,]+\s*¢|[\d,]+\s*dollar|[\d,]+\s*Euro";
List<String> lstPrice = new ArrayList<>();
Pattern rPrice = Pattern.compile(pricePattern);
Matcher mPrice = rPrice.matcher(caption);
while (mPrice.find()) {
lstPrice.add(mPrice.group());
}
if (lstPrice.size() > 0) {
return lstPrice.get(0);
}
return "";
}
你可以试试 replaceAll
Replaces every subsequence of the input sequence that matches the pattern with the given replacement string.
String pricePattern="2500$ 5405€ 554668¢ 885486¥ 588525dollar";
pricePattern= pricePattern.replaceAll("[^\d+]", " "); //2500 5405 554668 885486 588525
如果您需要 return 所有价格,请确保您的 getPrice
方法 returns List<String>
并调整正则表达式以匹配价格但仅捕获数字:
private List<String> getPrice(String caption) {
String pricePattern = "(?i)(\d[\d,]*)\s*(?:[$€¥¢]|dollar|Euro)";
List<String> lstPrice = new ArrayList<>();
Pattern rPrice = Pattern.compile(pricePattern);
Matcher mPrice = rPrice.matcher(caption);
while (mPrice.find()) {
lstPrice.add(mPrice.group(1));
}
return lstPrice;
}
String s = "dfdfm;lg 2500$ jshfsnefsfz5405€mnvkjdf64rfmkd554668¢ odsfrknegj 885486¥ dsflkef 588525dollar";
System.out.println(getPrice(s));
returns
[2500, 5405, 554668, 885486, 588525]
图案详情:
(?i)
- 不区分大小写的修饰符(嵌入标志选项)(\d[\d,]*)
- 第 1 组捕获一个数字,然后捕获 0+ 个数字或,
\s*
- 0+ 个空格(?:[$€¥¢]|dollar|Euro)
-$
、€
、¥
、¢
、dollar
或euro
(不区分大小写通过(?i)
) 启用搜索