首页 > 解决方案 > 正则表达式查找所有美元符号、括号和逗号

问题描述

我想要一个正则表达式来删除美元符号、逗号以及左括号和右括号的所有实例,以便可以将 String 解析为 Double。

例子是:

($108.34)
$39.60
1,388.80

编码:

@Parsed
@Replace(expression = "", replacement = "")
public Double extdPrice;

标签: javaregex

解决方案


这可能会有所帮助,我们删除此列表中的所有元素: , $ ( )

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Example {
    public static void main(String[] args) {
        final String regex = "[(),$]";
        final String string = "($108.34)\n"
     + "$39.60\n"
     + "1,388.80";
        final String subst = "";
        
        final Pattern pattern = Pattern.compile(regex);
        final Matcher matcher = pattern.matcher(string);
        
        // The substituted value will be contained in the result variable
        final String result = matcher.replaceAll(subst);
        
        System.out.println("Substitution result: " + result);
    }
}


推荐阅读