-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPhone.java
More file actions
51 lines (44 loc) · 1.33 KB
/
Phone.java
File metadata and controls
51 lines (44 loc) · 1.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// 1 и 2 задания
import java.util.regex.*;
public class Phone {
public static boolean check(String number){
try{
Pattern P = Pattern.compile("\\+\\d{1,3}\\s?\\(\\d{2,8}\\)\\s?((\\d{1,6}\\-){2})\\d{1,6}");
Matcher M = P.matcher(number);
boolean b = false;
while(M.find()){
b = true;
System.out.println(M.group());
}
return b;
}
catch (PatternSyntaxException e) {
System.out.println("Wrong regexp pattern");
return false;
}
}
public static String change(String number){
try{
Pattern P = Pattern.compile("\\s|\\(|\\)|\\-");
Matcher M = P.matcher(number);
boolean b = false;
if (check(number)){
while(M.find()){
number = M.replaceAll("");
b = true;
}
}
else{
return "wrong number";
}
return number;
}
catch (PatternSyntaxException e) {
System.out.println("Wrong regexp pattern");
return "";
}
}
public static void main(String[] args) {
System.out.println(change("+7(843)521-21-21"));
}
}