java - 正则表达式查找所有美元符号、括号和逗号
问题描述
我想要一个正则表达式来删除美元符号、逗号以及左括号和右括号的所有实例,以便可以将 String 解析为 Double。
例子是:
($108.34)
$39.60
1,388.80
编码:
@Parsed
@Replace(expression = "", replacement = "")
public Double extdPrice;
解决方案
这可能会有所帮助,我们删除此列表中的所有元素: , $ ( )
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);
}
}
推荐阅读
- tomcat - 如何通过 Windows 服务器中的 IP 地址使 Apache Tomcat 服务器可访问
- c - 在 C 中显示最高和最低数值
- angular - 为什么 Angular 应用程序在生产构建后抛出错误?
- c++ - `std::sample()` 的输出序列是否遵循输入序列的顺序?
- python-3.x - 使用快速排序对区间进行模糊排序
- mongodb - 更新 MongDB 中数组的最后一个元素
- docker - 使用端口 1376 启动 docker swarm 后,无法 docker init 或 docker leave
- python - 如何遍历城市列表以计算它们之间的距离
- macos - 为什么现在自制软件中没有很多不同公式的安装选项?
- css - 为什么 flex-item 的 z-index 不能与 Safari 中的“溢出:隐藏”一起使用?