Guest User

Untitled

a guest
Nov 9th, 2021
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.89 KB | None | 0 0
  1. package ru.utils;
  2.  
  3. import java.util.regex.Matcher;
  4. import java.util.regex.Pattern;
  5.  
  6. public class IPUtils {
  7.  
  8. public static boolean isValidIPAddress(String ip)
  9. {
  10.  
  11. String zeroTo255
  12. = "(\\d{1,2}|(0|1)\\"
  13. + "d{2}|2[0-4]\\d|25[0-5])";
  14.  
  15. String regex
  16. = zeroTo255 + "\\."
  17. + zeroTo255 + "\\."
  18. + zeroTo255 + "\\."
  19. + zeroTo255;
  20.  
  21. Pattern p = Pattern.compile(regex);
  22.  
  23. // If the IP address is empty
  24. // return false
  25. if (ip == null) {
  26. return false;
  27. }
  28.  
  29. // Pattern class contains matcher() method
  30. // to find matching between given IP address
  31. // and regular expression.
  32. Matcher m = p.matcher(ip);
  33.  
  34. // Return if the IP address
  35. // matched the ReGex
  36. return m.matches();
  37. }
  38. }
Advertisement
Add Comment
Please, Sign In to add comment