-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathSolution2.java
More file actions
51 lines (42 loc) · 1.25 KB
/
Solution2.java
File metadata and controls
51 lines (42 loc) · 1.25 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
51
/**
* Problem statement:
* Given a string, return the first valid US phone number in the string. If none is present, return "NONE".
* Valid patterns of US phone numbers are "123-456-7890" and "(123) 456-7890".
*
* Input 1:
* xx000-000-1111xxx
* Output 1:
* 000-000-1111
*
* Input 2:
* 123-456-7890(000) 000-0000
* Output 2:
* 123-456-7890
*
* Input 3:
* 1234567890
* Output 3:
* NONE
*/
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Solution2 {
static String find_phone_number(String text) {
boolean match = Pattern.matches(
"[0-9][0-9][0-9]-[0-9][0-9][0-9]-[0-9][0-9][0-9][0-9][a-z0-9]"
, text);
String pattern1 = "[0-9][0-9][0-9]-[0-9][0-9][0-9]-[0-9][0-9][0-9][0-9]";
String pattern2 = "[(][0-9][0-9][0-9][)]\\s[0-9][0-9][0-9]-[0-9][0-9][0-9][0-9]";
Pattern pattern = Pattern.compile(pattern1);
Matcher matcher = pattern.matcher(text);
if(matcher.find()) {
return matcher.group(0);
}
return "NONE";
}
public static void main(String[] args) {
String text1 = "xx000-000-1111xxx";
String text = " 123-456-7890(000) 000-0000 ";
System.out.println(find_phone_number(text));
}
}