有一个字符串"12.2A12W99.0Z0.123Q9“,我需要找到3组:(双精度或整数)(非数字)(双精度或整数),所以在示例中,我希望发生这种情况:
matcher.group (1) = "12.2" matcher.group (2) = "A" matcher.group (3) = "12" 我当前的正则表达式只匹配整数:"^ (\d+) (\D)(\d+)“,所以我希望将group (\d+)更改为匹配整数或双精度的值。
我根本不理解正则表达式,所以把我解释成5岁是很酷的。
发布于 2013-08-24 13:21:01
尝试下面的代码:-您的正则表达式只匹配数字字符。为了也匹配小数点,您需要:
Pattern.compile("\\d+\\.\\d+")
private Pattern p = Pattern.compile("\\d+(\\.\\d+)?");这个。是转义的,因为这将匹配未转义时的任何字符。
注意:这将只匹配带有小数点的数字,这就是您的示例中的数字。
private Pattern p = Pattern.compile("\\d+(\\.\\d+)?");
public void testInteger() {
Matcher m =p.matcher("10");
assertTrue(m.find());
assertEquals("10", m.group());
}
public void testDecimal() {
Matcher m =p.matcher("10.99");
assertTrue(m.find());
assertEquals("10.99", m.group());
}https://stackoverflow.com/questions/18415065
复制相似问题