正则表达式找到一个浮点数

我之前从未使用过正则表达式,但是这个java函数需要它(如下所示: 如何设置Edittext视图只允许两个数值和两个十进制值,如##。## )

我基本上只需要从文本框中获取一个浮点数,应该很简单。 我使用了一个工具,它说这应该工作:

String re1="([+-]?\\d*\\.\\d+)(?![-+0-9\\.])"; 

但它似乎没有工作,它不允许我在文本框中放任何东西。

这样做的正确方法是什么? 谢谢

尝试这个:

 String re1="^([+-]?\\d*\\.?\\d*)$"; 

解决这个问题的正确方法不是使用正则表达式,而只是使用:

 try { Float.parseFloat(string) return true; } catch (NumberFormatException ex) { return false; } 

工作完全正常,是相同的代码,后来用于解析浮动,因此没有bug(或者如果不是我们手头有更大的问题)。

这是正确的方法:

 String floatRegexp="^([+-]?(\\d+\\.)?\\d+)$"; 

或者如果您还看到其他文本的中间:

 String floatRegexp="([+-]?(\\d+\\.)?\\d+)"; 

如果你不寻找减号/加号:

 String floatRegexp="^(\\d+\\.)?\\d+$"; 

在接受了接受的答案(以及其他两个相关的答案)之后,我决定提供一个新答案。 然后我发现ChristofferHammarström于2016年9月1日8点40分大部分隐藏了关于Paulpro答案的评论(已被接受)。

这是接受的答案:

String re1 =“^([+ – ]?\ d * \。?\ d *)$”;

这是Christoffer的评论:

这匹配空字符串,或单个点,或者加号或减号本身。

实际上,遵循OP要求的3个答案中没有一个做得很好,这就是他必须为其他类对象提供RE并允许最终用户键入任何有效的浮点数。

这是我的新答案。

总之,我的答案是[+-]?(\d+|\d+\.\d+|\.\d+|\d+\.)([eE]\d+)? 。 如果类对象可能会传递给无效的前导或尾随字符,则必须在开头添加^ ,并在表达式的末尾添加$

以下是我如何阅读上面写的表达式:

[+-]?

这意味着允许使用前导符号,但不是必需的。

( \d+ | \d+\.\d+ | \.\d+ | \d+\. )

我添加了空格,以便更容易看到发生了什么。 这意味着接受4种forms的普通接受的无符号非科学符号浮点数表达式。 许多计算机语言允许所有四种。 也许通过更多分组,表达式的这一部分可以被压缩但是以可读性为代价。

([eE]\d+)?

最后一部分意味着不需要允许科学记数符后缀。

这是所有代码。

 $ cat Tester.java | sed 's/^/ /' import java.util.*; import java.util.regex.*; public class Tester { private class TestVal { private String val; private boolean accepted; private boolean expect; private boolean tested; public TestVal (String testValue, boolean expectedResult) { val = testValue; expect = expectedResult; reset(); } public String getValue () { return val; } public boolean getExpect () { return expect; } public void reset () { tested = false; accepted = false; } public boolean getAccepted () { return accepted; } public boolean getTested () { return tested; } public void setAccepted (boolean newAccept) { tested = true; accepted = newAccept; } } private ArrayList tests = new ArrayList(); public void doRETest (Pattern re, TestVal tv) { boolean matches = re.matcher(tv.getValue()).matches(); boolean ok = matches == tv.getExpect(); String result = ok ? "success" : "fail"; System.out.println(String.format("%10s matches=%5s: %s", tv.getValue(), matches, result)); tv.setAccepted(ok); } private void testsSummary () { int skipped = 0; int passes = 0; int failures = 0; for (TestVal tv : tests) if (tv.getTested()) if (tv.getAccepted()) passes++; else failures++; else skipped++; System.out.println(String.format("\npassed %d tests, failed %d tests, and %d tests skipped\n\n", passes, failures, skipped)); } public void doRETests (String re) { Pattern p = Pattern.compile(re); System.out.println(String.format("testing %s", re)); for (TestVal tv : tests) { tv.reset(); doRETest(p, tv); } testsSummary(); } public Tester () { tests.add(new TestVal("1", true)); tests.add(new TestVal(".1", true)); tests.add(new TestVal("1.", true)); tests.add(new TestVal("1.0", true)); tests.add(new TestVal("+1", true)); tests.add(new TestVal("+.1", true)); tests.add(new TestVal("+1.", true)); tests.add(new TestVal("+1.0", true)); tests.add(new TestVal("-1", true)); tests.add(new TestVal("-.1", true)); tests.add(new TestVal("-1.", true)); tests.add(new TestVal("-1.0", true)); tests.add(new TestVal("1e2", true)); tests.add(new TestVal(".1e2", true)); tests.add(new TestVal("1.e2", true)); tests.add(new TestVal("1.0e2", true)); tests.add(new TestVal("1.0e2.3", false)); tests.add(new TestVal(".", false)); tests.add(new TestVal("", false)); tests.add(new TestVal("+", false)); tests.add(new TestVal("-", false)); tests.add(new TestVal("a", false)); } public static void main (String args[]) { Tester t = new Tester(); String paulpro = "^([+-]?\\d*\\.?\\d*)$"; String lucac = "^([+-]?(\\d+\\.)?\\d+)$"; String armaj = "\\d+\\.\\d+"; String j6t7 = "[+-]?(\\d+|\\d+\\.\\d+|\\.\\d+|\\d+\\.)([eE]\\d+)?"; t.doRETests(paulpro); t.doRETests(lucac); t.doRETests(armaj); t.doRETests(j6t7); } } 

这就是我执行时发生的事情。

 $ javac Tester.java && java Tester | sed 's/^/ /' testing ^([+-]?\d*\.?\d*)$ 1 matches= true: success .1 matches= true: success 1. matches= true: success 1.0 matches= true: success +1 matches= true: success +.1 matches= true: success +1. matches= true: success +1.0 matches= true: success -1 matches= true: success -.1 matches= true: success -1. matches= true: success -1.0 matches= true: success 1e2 matches=false: fail .1e2 matches=false: fail 1.e2 matches=false: fail 1.0e2 matches=false: fail 1.0e2.3 matches=false: success . matches= true: fail matches= true: fail + matches= true: fail - matches= true: fail a matches=false: success passed 14 tests, failed 8 tests, and 0 tests skipped testing ^([+-]?(\d+\.)?\d+)$ 1 matches= true: success .1 matches=false: fail 1. matches=false: fail 1.0 matches= true: success +1 matches= true: success +.1 matches=false: fail +1. matches=false: fail +1.0 matches= true: success -1 matches= true: success -.1 matches=false: fail -1. matches=false: fail -1.0 matches= true: success 1e2 matches=false: fail .1e2 matches=false: fail 1.e2 matches=false: fail 1.0e2 matches=false: fail 1.0e2.3 matches=false: success . matches=false: success matches=false: success + matches=false: success - matches=false: success a matches=false: success passed 12 tests, failed 10 tests, and 0 tests skipped testing \d+\.\d+ 1 matches=false: fail .1 matches=false: fail 1. matches=false: fail 1.0 matches= true: success +1 matches=false: fail +.1 matches=false: fail +1. matches=false: fail +1.0 matches=false: fail -1 matches=false: fail -.1 matches=false: fail -1. matches=false: fail -1.0 matches=false: fail 1e2 matches=false: fail .1e2 matches=false: fail 1.e2 matches=false: fail 1.0e2 matches=false: fail 1.0e2.3 matches=false: success . matches=false: success matches=false: success + matches=false: success - matches=false: success a matches=false: success passed 7 tests, failed 15 tests, and 0 tests skipped testing [+-]?(\d+|\d+\.\d+|\.\d+|\d+\.)([eE]\d+)? 1 matches= true: success .1 matches= true: success 1. matches= true: success 1.0 matches= true: success +1 matches= true: success +.1 matches= true: success +1. matches= true: success +1.0 matches= true: success -1 matches= true: success -.1 matches= true: success -1. matches= true: success -1.0 matches= true: success 1e2 matches= true: success .1e2 matches= true: success 1.e2 matches= true: success 1.0e2 matches= true: success 1.0e2.3 matches=false: success . matches=false: success matches=false: success + matches=false: success - matches=false: success a matches=false: success passed 22 tests, failed 0 tests, and 0 tests skipped 

这个答案来自JohanSjöberghttps : //stackoverflow.com/a/12235002/4035655

您可以尝试使用正则表达式匹配数字

 \\d+\\.\\d+ 

这可能看起来像

 Pattern p = Pattern.compile("\\d+\\.\\d+"); Matcher m = p.matcher("Sstring>26.0.[2.3.2.3D] .352.(f) 1)"503B"(\1.67>>Sstring"); while (m.find()) { System.out.println(Float.parseFloat(m.group())); } 

输出是:

 26.0 2.3 2.4 1.67 

字符串的[2.3.2.3D]部分分为两个单独的浮点数