Regex pattern for numbers with dots

前端 未结 4 1042
没有蜡笔的小新
没有蜡笔的小新 2021-01-13 08:58

I need a regex expression for this

any number then . and again number and .

So this is valid

1.3.164.1.2583.15.46
546.598.856.1.68.268.695.5         


        
4条回答
  •  梦谈多话
    2021-01-13 09:56

    Something like this should work:

    (\\d+\\.?)+

    Edit

    Yep, not clear from the description if a final . is allowed (assuming an initial one is not).

    If not:

    (\\d+\\.?)*\\d+ or \\d+(\\.\\d+)* (if that seems more logical)

    Test

    for (String test : asList("1.3.164.1.2583.15.46",
        "546.598.856.1.68.268.695.5955565", "5..........", "...56.5656"))
        System.out.println(test.matches("\\d+(\\.\\d+)*"));
    

    produces:

    true
    true
    false
    false
    

提交回复
热议问题