-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIPAddressValidator.java
More file actions
35 lines (29 loc) · 969 Bytes
/
Copy pathIPAddressValidator.java
File metadata and controls
35 lines (29 loc) · 969 Bytes
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
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* Author: Aleksey Alekseenko
* Date: 21.10.13
*/
public class IPAddressValidator{
private Pattern pattern;
private Matcher matcher;
private static final String IPADDRESS_PATTERN =
"^([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\." +
"([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\." +
"([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\." +
"([01]?\\d\\d?|2[0-4]\\d|25[0-5])" +
"/(0?[0-9]|[12][0-9]|3[012])$";
public IPAddressValidator(){
int newValue = 1;
pattern = Pattern.compile(IPADDRESS_PATTERN);
}
/**
* Validate ip address with regular expression
* @param ip ip address for validation
* @return true valid ip address, false invalid ip address
*/
public boolean validate(final String ip){
matcher = pattern.matcher(ip);
return matcher.matches();
}
}