fork(3) download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6. import java.util.regex.*;
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. public static void main (String[] args) throws java.lang.Exception
  11. {
  12. String regexp = "(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)";
  13. String goodIp = "192.168.0.3";
  14. String badIp = "192.168.0.3g";
  15. Pattern pattern = Pattern.compile(regexp);
  16. Matcher matcher = pattern.matcher(goodIp);
  17. String teg1 = "";
  18. String teg2 = "";
  19. if (matcher.find())
  20. {
  21. teg1 = matcher.group();
  22. System.out.println(teg1);
  23. }
  24. matcher = pattern.matcher(badIp);
  25. if (matcher.find())
  26. {
  27. teg2 = matcher.group();
  28. System.out.println(teg2);
  29. }
  30.  
  31.  
  32. }
  33. }
Success #stdin #stdout 0.11s 321600KB
stdin
Standard input is empty
stdout
192.168.0.3
192.168.0.3