java,校验邮箱的正则
时间: 2024-01-14 16:21:23 浏览: 132
java使用正则表达式判断邮箱格式是否正确的方法
以下是Java中校验邮箱的正则表达式的示例代码:
```java
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class EmailValidator {
private static final String EMAIL_REGEX = "^[A-Za-z0-9+_.-]+@[A-Za-z0-9.-]+$";
public static boolean validateEmail(String email) {
Pattern pattern = Pattern.compile(EMAIL_REGEX);
Matcher matcher = pattern.matcher(email);
return matcher.matches();
}
public static void main(String[] args) {
String email = "test@example.com";
boolean isValid = validateEmail(email);
System.out.println("Is email valid? " + isValid); // 输出:Is email valid? true
}
}
```
阅读全文