我已经看到了几个关于如何检索和删除分隔符之间的字符串的示例。但是,我创建了一个非常简单的测试用例,并且根据各种示例得到了错误的输出。为什么我要取回我想用作搜索源的字符串而不是我正在搜索的字符串?
String temp = "56.0 F (13.3C)";
String exp = "((^))";
String p = temp.replace(exp, "");
System.out.println("p=" + p);
我得到字符串56.0F (13.3C)
作为我的输出。
我期待着56.0F
回来。
我还尝试使用模式匹配器:
Pattern pattern = Pattern.compile(exp);
Matcher matcher = pattern.matcher(temp);
if (matcher.find()) {
System.out.println(matcher.groupCount());
for (int i=0; i<=matcher.groupCount(); i++) {
String groupStr = matcher.group(i);
System.out.println(groupStr);
}
}
2 是打印到控制台的内容
关于我做错了什么的任何想法?我已经看了几个小时了。谢谢!