我并不确定在Java中正确的句点正则表达式是什么。以下是我的一些尝试,但遗憾的是,它们都表示任何字符。
String regex = "[0-9]*[.]?[0-9]*";
String regex = "[0-9]*['.']?[0-9]*";
String regex = "[0-9]*["."]?[0-9]*";
String regex = "[0-9]*[\.]?[0-9]*";
String regex = "[0-9]*[\\.]?[0-9]*";
String regex = "[0-9]*.?[0-9]*";
String regex = "[0-9]*\.?[0-9]*";
String regex = "[0-9]*\\.?[0-9]*";
我需要的是实际的"."字符本身,有人有想法吗?
我真正想做的是编写一个用于非负实数(允许小数)的正则表达式。因此可能的情况是:12.2、3.7、2.、0.3、.89、19。
String regex = "[0-9]*['.']?[0-9]*";
Pattern pattern = Pattern.compile(regex);
String x = "5p4";
Matcher matcher = pattern.matcher(x);
System.out.println(matcher.find());
最后一行应该输出false,但它仍然输出true。我认为我的正则表达式有问题。
?
使其可选。这就是你想要的吗?句号只需用\.
即可,无需使用括号或其他任何符号。 - CfreakSystem.out.println("scary.wombat".replaceAll("\\.", "_"));
- Scary Wombatmatcher.find()
和matcher.matches()
之间的区别 -find()
不适用于完整的区域。 - Disc-Addict