Skip to content

Commit 5c71475

Browse files
committed
Add Check Email
1 parent a9b2420 commit 5c71475

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

Strings/CheckEmail.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package Strings;
2+
3+
import java.util.Objects;
4+
import java.util.regex.Pattern;
5+
6+
/**
7+
* Example of email address validation. Checks if a given string is a valid email or not.
8+
*/
9+
public class CheckEmail {
10+
11+
public static void main(String[] args) {
12+
assert isEmail("hello@gmail.com");
13+
assert isEmail("iamweasel@yahoo.com.mx");
14+
assert isEmail("coco@hotmail.com");
15+
assert !isEmail("coco@hotmail@google.com");
16+
assert !isEmail("www.google.com");
17+
assert !isEmail("hello world");
18+
assert !isEmail(null);
19+
}
20+
21+
/**
22+
* Checks if the given string is an email address
23+
*
24+
* @param stringToEvaluate the string to evaluate
25+
* @return {@code true} if stringToEvaluate is an email address, otherwise {@code false}
26+
*/
27+
public static boolean isEmail(String stringToEvaluate) {
28+
if(Objects.isNull(stringToEvaluate)) return false;
29+
String emailRegex = "^[A-Za-z0-9_+&*-]+(?:\\.[A-Za-z0-9_+&*-]+)*@(?:[A-Za-z0-9-]+\\.)+[A-Za-z]{2,7}$";
30+
return Pattern.compile(emailRegex)
31+
.matcher(stringToEvaluate)
32+
.matches();
33+
}
34+
}

0 commit comments

Comments
 (0)