用java语言写一个判断java文件名与邮箱格式是否正确的代码
时间: 2024-05-15 22:19:59 浏览: 217
以下是判断Java文件名与邮箱格式是否正确的Java代码:
```java
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) {
String fileName = "HelloWorld.java";
String email = "example@example.com";
if (isValidJavaFileName(fileName)) {
System.out.println("Java文件名格式正确");
} else {
System.out.println("Java文件名格式不正确");
}
if (isValidEmail(email)) {
System.out.println("邮箱格式正确");
} else {
System.out.println("邮箱格式不正确");
}
}
// 判断Java文件名是否正确
public static boolean isValidJavaFileName(String fileName) {
Pattern pattern = Pattern.compile("^[a-zA-Z_$][a-zA-Z\\d_$]*\\.java$");
Matcher matcher = pattern.matcher(fileName);
return matcher.matches();
}
// 判断邮箱格式是否正确
public static boolean isValidEmail(String email) {
Pattern pattern = Pattern.compile("^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\\.[a-zA-Z0-9_-]+)+$");
Matcher matcher = pattern.matcher(email);
return matcher.matches();
}
}
```
阅读全文