我有((?:[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/上运行了示例,但它从未在运行时找到匹配项。有什么想法??
答案 0 :(得分:2)
您应该运行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