forked from janbodnar/Java-Advanced
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJavaRegexEmail.java
More file actions
42 lines (33 loc) · 1.23 KB
/
JavaRegexEmail.java
File metadata and controls
42 lines (33 loc) · 1.23 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
package com.zetcode;
import java.util.Arrays;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
// Jane the 'Jane' string
// ^Jane 'Jane' at the start of a string
// Jane$ 'Jane' at the end of a string
// ^Jane$ exact match of the string 'Jane'
// [abc] a, b, or c
// [a-z] any lowercase letter
// [^A-Z] any character that is not a uppercase letter
// (Jane|Becky) Matches either 'Jane' or 'Becky'
// [a-z]+ one or more lowercase letters
// ^[98]?$ digits 9, 8 or empty string
// ([wx])([yz]) wy, wz, xy, or xz
// [0-9] any digit
// [^A-Za-z0-9] any symbol (not a number or a letter)
public class JavaRegexEmail {
public static void main(String[] args) {
List<String> emails = Arrays.asList("[email protected]",
"andy@yahoocom", "34234sdfa#2345", "[email protected]");
Pattern p = Pattern.compile("^[a-zA-Z0-9._-]+@[a-zA-Z0-9-]+\\.[a-zA-Z.]{2,18}$");
for (String email : emails) {
Matcher m = p.matcher(email);
if (m.matches()) {
System.out.printf("%s matches%n", email);
} else {
System.out.printf("%s does not match%n", email);
}
}
}
}