Skip to content

Commit 0e1296c

Browse files
committed
Regex for email, phone number, ip address
1 parent 77ab7ce commit 0e1296c

File tree

6 files changed

+179
-14
lines changed

6 files changed

+179
-14
lines changed

Diff for: README.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ Implementation of Algorithms in Java, Most of these names are picked up from Gee
7070
- [ ] Tower of Hanoi
7171

7272
9. [ ] **Regex**
73-
- [ ] Regex for US Phone Number
74-
- [ ] Regex for Email
73+
- [X] [Regex for US Phone Number](../master/src/com/deepak/algorithms/Regex/RegexForUSPhoneNumber.java)
74+
- [X] [Regex for Email](../master/src/com/deepak/algorithms/Regex/RegexForEmail.java)
75+
- [X] [Regex for IP Address](../master/src/com/deepak/algorithms/Regex/RegexForIPAddress.java)
7576
- [ ] Regex Pattern Matcher implementation

Diff for: src/com/deepak/algorithms/Regex/RegexForEmail.java

+63
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,68 @@
1+
/**
2+
* Algorithms-In-Java
3+
* RegexForEmail.java
4+
*/
15
package com.deepak.algorithms.Regex;
26

7+
import java.util.regex.Matcher;
8+
import java.util.regex.Pattern;
9+
10+
/**
11+
* Regular expression for email address
12+
*
13+
* @author Deepak
14+
*/
315
public class RegexForEmail {
416

17+
/**
18+
* Method to check for valid email
19+
*
20+
* @param args
21+
*/
22+
public static void main(String[] args) {
23+
System.out.println(matchEmail("[email protected]")); // true
24+
System.out.println(matchEmail("[email protected]")); // true
25+
System.out.println(matchEmail("[email protected]")); // true
26+
System.out.println(matchEmail("[email protected]")); // true
27+
System.out.println(matchEmail("ABC#123$xyz*[email protected]")); // true
28+
System.out.println(matchEmail("a [email protected]")); // false
29+
System.out.println(matchEmail("[email protected]")); // false
30+
System.out.println(matchEmail("[email protected]")); // false
31+
System.out.println(matchEmail("@example.com")); // false
32+
System.out.println(matchEmail("abc_xyz")); // false
33+
System.out.println(matchEmail("email@example..")); // false
34+
}
35+
36+
/**
37+
* Method to check for a valid email address
38+
* Explanation :
39+
* => The local part of email can accept any of these,
40+
* Upper case and lower case English alphabets (A-Z, a-z)
41+
* Digits 0-9
42+
* Characters ! # $ % & ' * + - / = ? ^ _ ` { | } ~
43+
* Character . (provided that it is not the first/last character) and it does not appear two or more times consecutively
44+
*
45+
* Explanation of Regular Expression :
46+
*
47+
* There will be 4 groups here,
48+
* For groups, we can use [A-Z0-9_!#$%&'*+/=?`{|}~^-]
49+
* ^ - start of the string
50+
* $ - end of the string
51+
*
52+
* So, first group + any character + second group + @ + third group + any character + fourth group
53+
* first group and second group will be same
54+
* third group and fourth group will be same
55+
*
56+
* And, we will be using case insensitive here, since we can have A-Z or a-z
57+
*
58+
* @param input
59+
* @return {@link boolean}
60+
*/
61+
public static boolean matchEmail(String input) {
62+
String regexforEmail = "^[A-Z0-9_!#$%&'*+/=?`{|}~^-]+(?:\\.[A-Z0-9_!#$%&'*+/=?`{|}~^-]+)*@[A-Z0-9-]+(?:\\.[A-Z0-9-]+)*$";
63+
Pattern pattern = Pattern.compile(regexforEmail, Pattern.CASE_INSENSITIVE);
64+
Matcher matcher = pattern.matcher(input);
65+
return matcher.matches();
66+
}
67+
568
}
+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/**
2+
* Algorithms-In-Java
3+
* RegexForIPAddress.java
4+
*/
5+
package com.deepak.algorithms.Regex;
6+
7+
import java.util.regex.Matcher;
8+
import java.util.regex.Pattern;
9+
10+
/**
11+
* Regular expression for IP Address
12+
*
13+
* @author Deepak
14+
*/
15+
public class RegexForIPAddress {
16+
17+
/**
18+
* Method to check for valid ip address
19+
* @param args
20+
*/
21+
public static void main(String[] args) {
22+
System.out.println(matchIPAddress("1.1.1.1")); // true
23+
System.out.println(matchIPAddress("255.255.255.255")); // true
24+
System.out.println(matchIPAddress("192.168.1.1")); // true
25+
System.out.println(matchIPAddress("127.0.0.1")); // true
26+
System.out.println(matchIPAddress("26.10.2.10")); // true
27+
System.out.println(matchIPAddress("10.10.10")); // false
28+
System.out.println(matchIPAddress("a.a.a.a")); // false
29+
System.out.println(matchIPAddress("999.10.10.20")); // false
30+
System.out.println(matchIPAddress("22.2222.22.2")); // false
31+
System.out.println(matchIPAddress("127.0.0.310")); // false
32+
System.out.println(matchIPAddress("127.0.0.1101")); // false
33+
}
34+
35+
/**
36+
* Method to check for a valid IP Address
37+
* Explanation :
38+
* => A valid IP address will have 4 sets,
39+
* [0-255].[0-255].[0-255].[0-255]
40+
*
41+
* Explanation of Regular Expression :
42+
*
43+
* There will be 4 groups here,
44+
* ^ - start of the line
45+
* ( - start of group #1
46+
* [01]?\\d\\d? - Can be one or two digits. If three digits appear, it must start either 0 or 1
47+
* e.g ([0-9], [0-9][0-9],[0-1][0-9][0-9])
48+
* | - ...or
49+
* 2[0-4]\\d - start with 2, follow by 0-4 and end with any digit (2[0-4][0-9])
50+
* | - ...or
51+
* 25[0-5] - start with 2, follow by 5 and ends with 0-5 (25[0-5])
52+
* ) - end of group #2
53+
* \. - follow by a dot "."
54+
* .... - repeat with 3 times (3x)
55+
* $ - end of the line
56+
*
57+
* @param input
58+
* @return {@link boolean}
59+
*/
60+
public static boolean matchIPAddress(String input) {
61+
String regexforIPAddress = "^([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.([01]?\\d\\d?|2[0-4]\\d|25[0-5])$";
62+
Pattern pattern = Pattern.compile(regexforIPAddress);
63+
Matcher matcher = pattern.matcher(input);
64+
return matcher.matches();
65+
}
66+
67+
}

Diff for: src/com/deepak/algorithms/Regex/RegexForUSPhoneNumber.java

+44-10
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,57 @@
1+
/**
2+
* Algorithms-In-Java
3+
* RegexForUSPhoneNumber.java
4+
*/
15
package com.deepak.algorithms.Regex;
26

37
import java.util.regex.Matcher;
48
import java.util.regex.Pattern;
59

10+
/**
11+
* Regular expression for US Phone Numbers
12+
*
13+
* @author Deepak
14+
*/
615
public class RegexForUSPhoneNumber {
7-
16+
17+
/**
18+
* Main method to test the program
19+
*
20+
* @param args
21+
*/
822
public static void main(String[] args) {
9-
System.out.println(matchUSPhoneNumber("425-633-6014"));
10-
System.out.println(matchUSPhoneNumber("425-633-601"));
11-
System.out.println(matchUSPhoneNumber("425-6336014"));
12-
System.out.println(matchUSPhoneNumber("(425)-633-6014"));
13-
System.out.println(matchUSPhoneNumber("42-33-6014"));
14-
System.out.println(matchUSPhoneNumber("-633-6014"));
15-
System.out.println(matchUSPhoneNumber("6014"));
16-
System.out.println(matchUSPhoneNumber("425-633-60145"));
23+
System.out.println(matchUSPhoneNumber("4256336014")); // true
24+
System.out.println(matchUSPhoneNumber(" 4256336014")); // false
25+
System.out.println(matchUSPhoneNumber("425-633-6014")); // true
26+
System.out.println(matchUSPhoneNumber("425-633-601")); // false
27+
System.out.println(matchUSPhoneNumber("425-6336014")); // true
28+
System.out.println(matchUSPhoneNumber("(425)-633-6014")); // true
29+
System.out.println(matchUSPhoneNumber("42-33-6014")); // false
30+
System.out.println(matchUSPhoneNumber("-633-6014")); // false
31+
System.out.println(matchUSPhoneNumber("6014")); // false
32+
System.out.println(matchUSPhoneNumber("425-633-60145")); // false
1733
}
18-
34+
1935
/**
2036
* Method to match US phone number
37+
* Explanation :
38+
* => Phone number can be in below format
39+
* 1. First part should have 3 digits
40+
* 2. Second part should have 3 digits
41+
* 3. Third part should have 4 digits
42+
* => Now, there could be various possibilities in between,
43+
* i.e we may have a -, a space, no white space or a braces ()
44+
*
45+
* Explanation of Regular Expression :
46+
*
47+
* There will be 3 groups since I have 3 different blocks in my input.
48+
* For groups, we can use [0-9]{3} or [0-9]{4}
49+
* ^ - start of the string
50+
* $ - end of the string
51+
* \\ - matches the character literally
52+
* () - capture everything that is enclosed
53+
* ? Quantifier - matches between 0 and 1 times
54+
* [-.\\s] - Match anything out of these
2155
*
2256
* @param input
2357
* @return {@link boolean}

Diff for: src/com/deepak/algorithms/Searching/BinarySearch.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Algorithms-in-Java
2+
* Algorithms-In-Java
33
* BinarySearch.java
44
*/
55
package com.deepak.algorithms.Searching;

Diff for: src/com/deepak/algorithms/Searching/LinearSearch.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Algorithms-in-Java
2+
* Algorithms-In-Java
33
* LinearSearch.java
44
*/
55
package com.deepak.algorithms.Searching;

0 commit comments

Comments
 (0)