Regex for latitude with required 6 decimal places

Multi tool use
Multi tool use


Regex for latitude with required 6 decimal places



I need a regex in Java that will check if a String representation of a double has required 6 decimal places. Before the decimal point, value can be positive or negative.


1.123456 - correct
-123123123.123456 - correct
123123123.123456 - correct
-123123123.123456 - correct

1.12345 - wrong
-.123456 - wrong
.123456 - wrong
.12345 - wrong
123456 - wrong



I tried:


^s*(?=.*[1-9])d*(.d{6})?s*$



but it doesn't cover all edges.





If you require 6 decimal places, why have you made decimal places optional?
– Bohemian
Jul 1 at 15:07






Immediately I can imagine an edge case here. What happens if a lat/lng value only has 4 digits, because everything from the fifth digit onward is known to be zero and does not appear? Then, would you still require 6 digits always?
– Tim Biegeleisen
Jul 1 at 15:10





The double type and "decimal places" are two concepts which (perhaps surprisingly) have basically nothing to do with each others. Perhaps instead of double you mean string containing decimal number? Converting between that and double value is lossy operation.
– hyde
Jul 1 at 15:22






Is the double a "double string"? It is not clear whenever the double is a double string or a double literal.
– Mulliganaceous
Jul 2 at 21:45






Furthermore, what counts as "correct" and "wrong" in your question? You should give your current and expected results to support the "correctness" and "wrongness" of these digit strings. The question has been downvoted so urgent attention is required.
– Mulliganaceous
Jul 3 at 17:31




3 Answers
3



Try this:


^s*(-|+)?(0|[1-9]d*).d{6}s*$



See live demo.



This allows the first digits to be zero only if it's the only digit before the dot, eg 0.123456 is OK, but not 01.123456. .d{6} requires exactly 6 decimal places.


0.123456


01.123456


.d{6}



The valid input should


^s*


-


+


(-|+)?


d+


.


(d{6})


^s*



Try this:


^s*(-|+)?d+.(d{6})s*$



In your regex the positive lookahead (?=.*[1-9]) asserts that what is on the right side should contain a digit which will succeed for all examples. After that assertion you match zero or more digits d* followed by a part that optionally matches a dot and 6 digits (.d{6})? so this will match .588888 or also 1.


(?=.*[1-9])


d*


(.d{6})?


.588888


1



If you want to match an optional minus sign you could use -?


-?



For your example data you might use:



^-?d+.d{6}$


^-?d+.d{6}$



In Java:


String regex = "^-?d+.d{6}$";



Explanation


^


-?


d+.d{6}


$



Demo






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

TK AZLGBtroPo5LQblXUNt3LnuE 3DTnGwo4EQL5oL4BkMTCfr 1mM5Y 4 90bBSk93,CQIyaW,ZfMH0Y5gpFPN
E9hosqgTvo9BQdgj hVjkO2CHkXIKh,K4163tlL2G5wu1ttpGr,9ZaA0R

Popular posts from this blog

PHP contact form sending but not receiving emails

Do graphics cards have individual ID by which single devices can be distinguished?

Create weekly swift ios local notifications