fork download
  1. import java.util.*;
  2. import java.lang.*;
  3. import java.io.*;
  4.  
  5. class Ideone
  6. {
  7. public static void main (String[] args) throws java.lang.Exception
  8. {
  9. String strs[] = {
  10. "+1-555-5555",
  11. "+12-5555555",
  12. "+123-(555) 555-5555",
  13. "+1(555) 555-5555",
  14. "5555555555",
  15. "+5555555555",
  16. "+123-)(----"
  17. };
  18. String regex = "\\+\\d{1,3}-(?:\\(\\d{3}\\)\\h*)?\\d{3}-?\\d{4}";
  19. for (String s : strs)
  20. System.out.printf("%s --> %s%n", s, s.matches(regex) ? "Match" : "No match");
  21. }
  22. }
Success #stdin #stdout 0.08s 34212KB
stdin
Standard input is empty
stdout
+1-555-5555 --> Match
+12-5555555 --> Match
+123-(555) 555-5555 --> Match
+1(555) 555-5555 --> No match
5555555555 --> No match
+5555555555 --> No match
+123-)(---- --> No match