韩顺平老师 B站
习题练习
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/c9f7c0cea207678b7b8a4533f305c1b8.png)
public static void main(String[] args) {
String content = "wu6426-miao@org.com";
if(content.matches("^[\\w-]+@([a-zA-Z]+\\.)+[a-zA-Z]+$")){
System.out.println("合法");
}else{
System.out.println("不合法");
}
}
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/64dfc3a6152fe0b7e7514cb175966a5f.png)
public static void main(String[] args) {
String content = "-19";
if(content.matches("^(-)?\\d+(\\.)\\d+$")){
System.out.println("小数");
}else{
System.out.println("整数");
}
}
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/47bf0961742990a4613f5dec970423dc.png)
public static void main(String[] args) {
String content = "http://www.sohu.com:8080/abc/index.htm";
Pattern pattern = Pattern.compile("^([a-z]+)://([a-z.]+[a-z]):(\\d+)(/[a-z]+/)([a-z.]+[a-z])$");
Matcher matcher = pattern.matcher(content);
while(matcher.find()){
System.out.println("协议为:" + matcher.group(1));
System.out.println("域名为:" + matcher.group(2));
System.out.println("端口为:" + matcher.group(3));
System.out.println("文件名为:" + matcher.group(5));
}
}