forked from rampatra/Algorithms-and-Data-Structures-in-Java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRegexValidateLatLong.java
43 lines (36 loc) · 1.06 KB
/
RegexValidateLatLong.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package com.rampatra.misc;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* @author rampatra
* @since 9/12/15
*/
public class RegexValidateLatLong {
/**
* Validates latitude/longitude in the form (+75, 180) etc.
*
* @param s
* @return
*/
public static String validateLatLong(String s) {
String regex_coords = "^(\\(\\-?\\d+(\\.\\d+)?),\\s*(\\-?\\d+(\\.\\d+)?\\))$";
Pattern compiledPattern2 = Pattern.compile(regex_coords, Pattern.CASE_INSENSITIVE);
Matcher matcher2 = compiledPattern2.matcher(s);
while (matcher2.find()) {
return "Valid";
}
return "Invalid";
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int t = Integer.parseInt(in.nextLine());
String[] in_ar = new String[t];
for (int i = 0; i < t; i++) {
in_ar[i] = in.nextLine();
}
for (String i : in_ar) {
System.out.println(validateLatLong(i));
}
}
}