我有 ((?:[0-9]{1,3}[\.,]?)*[\.,]?[0-9]+)
进行过滤在 java 上以字符串形式输出价格,所以我将它们放在这样的位置:
public static final String new_price = "((?:[0-9]{1,3}[\\.,]?)*[\\.,]?[0-9]+)";
final Pattern p = Pattern.compile(new_price, 0);
final Matcher m = p.matcher(label);
if (m.matches()) {
Log.d(TAG, "found! good start");
if (m.groupCount() == 1) {
Log.d(TAG, "start match price" + " : " + m.group(0));
}
if (m.groupCount() == 2) {
Log.d(TAG, "start match price" + " : " + m.group(1));
}
}
我得到了样本 http://www.regexr.com/但它从未在运行时找到匹配项。有什么想法吗??
最佳答案
而不是使用 matches()
你应该运行 m.find()
它搜索下一个匹配项(这应该在 while
循环中完成!):
String new_price = "((?:[0-9]{1,3}[\\.,]?)*[\\.,]?[0-9]+)";
String label = "$500.00 - $522.30";
final Pattern p = Pattern.compile(new_price, 0);
final Matcher m = p.matcher(label);
while (m.find()) {
System.out.println("found! good start");
if (m.groupCount() == 1) {
System.out.println("start match price" + " : " + m.group(0));
}
if (m.groupCount() == 2) {
System.out.println("start match price" + " : " + m.group(1));
}
}
输出
found! good start
start match price : 500.00
found! good start
start match price : 522.30
关于java - 过滤掉Java中的价格或成本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28824180/