File tree Expand file tree Collapse file tree 1 file changed +34
-0
lines changed Expand file tree Collapse file tree 1 file changed +34
-0
lines changed Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments