Java-正则表达式

概述

  • 在讲正则表达式之前,让我们先完成一个小程序
  • 需求:定义一个功能对QQ号进行校验。
  • 要求:长度5~15. 只能是数字, 0不能开头
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
//太复杂
public static void checkQQ(String qq){

int len = qq.length();
if(len>=5 && len<=15){
if(!qq.startsWith("0")){
try {
long l = Long.parseLong(qq);
System.out.println(l+":正确");
}catch(NumberFormatException e){
System.out.println(qq+":含有非法字符");
}
}else{
System.out.println(qq+":不能0开头");
}
}else{
System.out.println(qq+":长度错误");
}
}
1
2
3
4
String qq = "12346578";
String regex = "[1-9][0-9]{4,14}";//正则表达式
boolean b = qq.matches(regex);
System.out.println(qq+":"+b);
  • 你能想象到,之前那么多行的程序,可以被短短几行代码表达出来吗?
  • 正则表达式用于操作字符串数据,通过一些特定的符号来体现的。
  • 所以我们为了掌握正则表达式,必须要学习一些符号。虽然简化了,但是阅读性差

规则

  • 可以选择看API文档中 java.util.regex 目录下的Pattern类,或者下面的连接教程

正则表达式-语法

常用功能

匹配

  • 使用的就是String类中的matches方法
  • 假设手机号只有13***,15***,18***这几个频段
1
2
3
4
5
6
7
public static void functionDemo_1(){	
//匹配手机号码是否正确。
String tel = "15800001111";
String regex = "1[358]\\d{9}";
boolean b = tel.matches(regex);
System.out.println(tel+":"+b);
}

切割

  • 使用的就是String类中的split方法
  • 需求:想要以连续的出现的任意相同字母为切割符
  • 那么就是用”.“ 来表示任意字符,它后面的字符应该是跟他一样的,所以把 . 用小括号括起来
  • 这就是组,它是字符串中第一组被包起来的,所以后面用 “\\1”表示它,+代表可以出现1或多次
1
2
3
4
5
6
7
8
9
10
11
public static void functionDemo_2(){

String str = "zhangsanttttxiaoqiangmmmmmmzhaoliu";

String[] names = str.split("(.)\\1+");

for(String name : names){
System.out.println(name);
}

}
  • 那么 ( ( a ) ( b ( c ) ) ) 分别是几组那?
  • 第一组: ( ( a ) ( b ( c ) ) )
  • 第二组: \a
  • 第三组: ( b ( c ) )
  • 第四组: ( c )

替换

  • 使用的就是String类中的 replaceAll() 方法
  • 需求1:将连续出现的字符,替换为一个该字符
  • 需求2:输入手机号,将第4-7位替换为*
1
2
3
4
5
6
7
8
9
10
11
12
13
14
public static void functionDemo_3() {

String str = "zhangsanttttxiaoqiangmmmmmmzhaoliu";
// $代表前一个参数,$1 前一个参数的第一组
str = str.replaceAll("(.)\\1+", "$1");
System.out.println(str);

String tel = "15800001111";//158****1111;
// 将手机号切割,前三位,后四位各为一组
tel = tel.replaceAll("(\\d{3})\\d{4}(\\d{4})", "$1****$2");

System.out.println(tel);

}

获取

  • 将正则规则进行对象的封装
  • 通过正则对象的matcher方法字符串相关联。获取要对字符串操作的匹配器对象Matcher
  • 通过Matcher匹配器对象的方法对字符串进行操作
1
2
3
Pattern p = Pattern.compile("a*b");
Matcher m = p.matcher("aaaaab");
boolean b = m.matches();
  • 需求:获取字符串中的 三个字符的单词
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public  static void functionDemo_4() {

String str = "da jia hao,ming tian bu fang jia!";

String regex = "\\b[a-z]{3}\\b";

//1,将正则封装成对象。
Pattern p = Pattern.compile(regex);
//2, 通过正则对象获取匹配器对象。
Matcher m = p.matcher(str);

//使用Matcher对象的方法对字符串进行操作。
//既然要获取三个字母组成的单词
//查找。 find();
System.out.println(str);
while(m.find()){
System.out.println(m.group());//获取匹配的子序列

System.out.println(m.start()+":"+m.end());
}
}

练习

  • 1、治疗口吃:我我…我我…我我我爱…爱爱爱爱…爱爱..学学学学学…学学..习习习习.习
  • 2、对IP地址排序。 192.168.10.34 127.0.0.1 3.3.3.3 105.70.11.55
  • 3、对邮件地址校验。
1
2
3
4
5
6
7
8
9
10
11
12
13
//练习1
public static void test_1(){

String str = "我我...我我...我我我爱...爱爱爱爱...爱爱..学学学学学...学学..习习习习.习";
//1,将字符串中.去掉 用替换。
str = str.replaceAll("\\.+", "");
System.out.println(str);

//2,替换叠词。
str = str.replaceAll("(.)\\1+", "$1");
System.out.println(str);

}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
//练习2
public static void test_2(){

String ip_str = "192.168.10.34 127.0.0.1 3.3.3.3 105.70.11.55";

//1,为了让ip可以按照字符串顺序比较,只要让ip的每一段的位数相同。
//所以,补零,按照每一位所需做多0进行补充。每一段都加两个0.
ip_str = ip_str.replaceAll("(\\d+)", "00$1");
System.out.println(ip_str);

//然后每一段保留数字3位。
ip_str = ip_str.replaceAll("0*(\\d{3})", "$1");
System.out.println(ip_str);

//2、将ip地址切出。
String[] ips = ip_str.split(" +");

TreeSet<String> ts = new TreeSet<String>();

for(String ip : ips){
ts.add(ip);
}

for(String ip : ts){
System.out.println(ip.replaceAll("0*(\\d+)", "$1"));
}

}
1
2
3
4
5
6
7
8
9
10
11
12
//练习3
public static void test_3() {

String mail = "abc1@sina.com.cn";

String regex = "^[A-Za-z0-9\u4e00-\u9fa5]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$";

boolean b = mail.matches(regex);

System.out.println(mail+":"+b);

}

练习(爬取邮箱)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
public class RegexTest2 {

public static void main(String[] args) throws IOException {

List<String> list = getMailsByWeb();

for(String mail : list){
System.out.println(mail);
}
}

public static List<String> getMailsByWeb() throws IOException {

//1,读取源文件。
URL url = new URL("http://192.168.1.100:8080/myweb/mail.html");

BufferedReader bufIn = new BufferedReader(new InputStreamReader(url.openStream()));

//2,对读取的数据进行规则的匹配。从中获取符合规则的数据.
String mail_regex = "\\w+@\\w+(\\.\\w+)+";

List<String> list = new ArrayList<String>();

Pattern p = Pattern.compile(mail_regex);

String line = null;

while((line=bufIn.readLine())!=null){

Matcher m = p.matcher(line);
while(m.find()){
//3,将符合规则的数据存储到集合中。
list.add(m.group());
}
}
return list;
}

//爬取本地文件
public static List<String> getMails() throws IOException{
//1,读取源文件。
BufferedReader bufr = new BufferedReader(new FileReader("c:\\mail.html"));
//2、剩下的和上面的一样了(从第二步骤开始)
//记得将 bufIn 改为 bufr
}
}