Regular expression pattern for dot numbers.

I need a regex expression for this

any number. and again the number and.

So it really is

1.3.164.1.2583.15.46 546.598.856.1.68.268.695.5955565 

but

 5.......... ...56.5656 

invalid

I tried templates like:

 pattern = "[0-9](\\.[0-9]?*)?*"; pattern = "[0-9](\\.[0-9]?*)?$"; pattern = "[^0-9\\.]"; 

but not one of them meets my requirement. Please, help?

My existing code

 String PATTERN="\\d+(\\.\\d+)*"; @Override public void insertString(int arg0, String arg1, AttributeSet arg2) { if(!arg1.matches(this.PATTERN)) return; super.insertString(arg0, arg1, arg2); } 
+4
source share
3 answers

Something like this should work:

(\\d+\\.?)+

Edit

Yes, it is not clear from the description if the final is allowed . (provided that the source 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+)*")); 

gives:

 true true false false 
+4
source

I was thinking of a recursive regex here, and my pattern is:

 pattern = "\\d+.\\d+(?:.\\d+.\\d+)*" 
0
source

This [0-9]+([.][0-9]+)* equivalent to \\d+([.]\\d+)* valid for

1.3.164.1.2583.15.46 , 546.598.856.1.68.268.695.5955565 and 5465988561682686955955565

And this [0-9]+([.][0-9]+)+ equivalent to \\d+([.]\\d+)+ valid for

1.3.164.1.2583.15.46 and 546.598.856.1.68.268.695.5955565 , but not for 5465988561682686955955565

0
source

Source: https://habr.com/ru/post/1443582/


All Articles