주어진 입력 문자열이 유효한 이메일 ID인지 확인하려면 이메일 ID와 일치하는 정규식을 다음과 일치시킵니다. -
"^[a-zA-Z0-9+_.-]+@[a-zA-Z0-9.-]+$"
어디,
-
^는 문장의 시작과 일치합니다.
-
[a-zA-Z0-9+_.-]는 영문자(두 경우 모두), 숫자, "+", "_", "." 중 하나의 문자와 일치합니다. 그리고 @ 기호 앞의 "-".
-
+는 위에서 언급한 문자 집합이 한 번 이상 반복됨을 나타냅니다.
-
@는 자신과 일치합니다.
-
[a-zA-Z0-9.-]는 영문자(두 경우 모두), 숫자, "." 중 하나의 문자와 일치합니다. @ 기호 뒤의 "-".
-
$는 문장의 끝을 나타냅니다.
예
import java.util.Scanner;
public class ValidatingEmail {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter your Email: ");
String phone = sc.next();
String regex = "^[a-zA-Z0-9+_.-]+@[a-zA-Z0-9.-]+$";
//Matching the given phone number with regular expression
boolean result = phone.matches(regex);
if(result) {
System.out.println("Given email-id is valid");
} else {
System.out.println("Given email-id is not valid");
}
}
} 출력 1
Enter your Email: example.samplemail@gmail.com Given email-id is valid
출력 2
Enter your Email: sample?examplemail@gmail.com Given email-id is not valid
예시 2
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Test {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter your name: ");
String name = sc.nextLine();
System.out.println("Enter your email id: ");
String phone = sc.next();
//Regular expression to accept valid email id
String regex = "^[a-zA-Z0-9+_.-]+@[a-zA-Z0-9.-]+$";
//Creating a pattern object
Pattern pattern = Pattern.compile(regex);
//Creating a Matcher object
Matcher matcher = pattern.matcher(phone);
//Verifying whether given phone number is valid
if(matcher.matches()) {
System.out.println("Given email id is valid");
} else {
System.out.println("Given email id is not valid");
}
}
} 출력 1
Enter your name: vagdevi Enter your email id: sample.123@gmail.com Given email id is valid
출력 2
Enter your name: raja Enter your email id: raja$test@gmail.com Given email id is not valid