java - 过滤掉Java中的价格或成本

标签 java regex

我有 ((?:[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/

相关文章:

Ruby 正则表达式非捕获组

regex - 将二进制数分成零组和一组

java - 如何防止使用显示标签包装导出到 excel 的数据?

java - 将 JSONArray 转换为 POJO 类 List<type>,无法转换

java - 复制数组或强制 ArrayList 指定初始大小。什么更有效?

java - JVM 是否缓存汇编代码?

regex - 使用正则表达式验证手机号码

php - 正则表达式在扩展 PCRE (php) 后匹配一个空格

java - 等待...通知低级同步的最佳选择是什么?

java - 使用 Java Regex 删除字符串开头出现的给定字符序列