n4wn4w

REGEX

Apr 7th, 2015
295
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.84 KB | None | 0 0
  1. String text =
  2.                 "Hello, my number in Sofia is +359 894 11 22 33, " +
  3.                 "but in Munich my number is +49 89 975-99222.";
  4.             String phoneRegEx = "\\+\\d{1,3}([ -]*[0-9]+)+";
  5.             Pattern phonePattern = Pattern.compile(phoneRegEx);
  6.             Matcher matcher = phonePattern.matcher(text);
  7.             while (matcher.find()) {
  8.                 System.out.println(matcher.group());
  9.             }
  10.            
  11.             System.out.println("+359 2 981-981".matches(phoneRegEx));
  12.             System.out.println("invalid number".matches(phoneRegEx));
  13.             System.out.println("+359 123-".matches(phoneRegEx));
  14.             System.out.println("+359 (2) 981 981".matches(phoneRegEx));
  15.             System.out.println("+44 280 11 11".matches(phoneRegEx));
  16.             System.out.println("++44 280 11 11".matches(phoneRegEx));
  17.             System.out.println("(+49) 325 908 44".matches(phoneRegEx));
  18.             System.out.println("+49 325 908-40-40".matches(phoneRegEx));
Advertisement
Add Comment
Please, Sign In to add comment