fork(1) 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 rx = "[\\(]?\\d{3}[\\)]?([-.]?)\\s*\\d{3}\\1\\s*\\d{4}";
  13. System.out.println(getMatch("(123).123.1234", rx));
  14. System.out.println(getMatch("1234567890", rx));
  15. System.out.println(getMatch("(123)1231234", rx));
  16. System.out.println(getMatch("(123)-456-7890", rx));
  17. System.out.println(getMatch("(123) 123 1234", rx));
  18. System.out.println(getMatch("123 123 1234", rx));
  19. System.out.println(getMatch("(123). 456. 7890", rx));
  20. System.out.println(getMatch("(123)- 456- 7890", rx));
  21. // FALSE
  22. System.out.println(getMatch("123.123-1234", rx));
  23. System.out.println(getMatch("(123)-123.1234", rx));
  24. System.out.println(getMatch("123. 123- 1234 ", rx));
  25. }
  26. public static String getMatch(String str, String pattern)
  27. {
  28. Pattern ptrn = Pattern.compile(pattern);
  29. Matcher m = ptrn.matcher(str);
  30. if (m.find()) {
  31. return m.group();
  32. }
  33. else {
  34. return "";
  35. }
  36. }
  37. }
Success #stdin #stdout 0.1s 320256KB
stdin
Standard input is empty
stdout
(123).123.1234
1234567890
(123)1231234
(123)-456-7890
(123) 123 1234
123   123    1234
(123). 456. 7890
(123)- 456- 7890