Java笔记-正则

Java笔记 正则

初探正则

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public static void main(String[] args) {
String str = "1919年,一位教授在《新青年》上发表了一篇系统介绍马克思主义理论的文章," +
"热情赞颂马克思主义为“世界改造原动的学说”,并公开申明自己对马克思主义的信仰," +
"但同时也认为,不可将“马氏的学说”就“那样整个拿来,应用于我们生存的社会”。" +
"此后,他更是明确提出,对待马克思主义必须研究它“怎样应用于中国今日的政治经济情形”。" +
"这位教授便是中国共产党的主要创始人之一——李大钊。以科学的态度对待马克思主义," +
"视马克思主义是革命的科学,而不是抽象的学理和不变的教条,这样的态度,正是中国共产党人一以贯之的立场追求," +
"贯穿于我们党的百年奋斗历程。";
String rgx = "\\d\\d\\d\\d";
Pattern pattern = Pattern.compile(rgx);
Matcher matcher = pattern.matcher(str);
while (matcher.find()){
System.out.println(matcher.group(0));
}

}

image-20210606101110770

找到后,将子字符串的开始索引记录到matcher对象的属性int[] groups的groups[0]=0中,将结束的索引+1记录到groups[1]=4中。

同时记录oldLast的值,记录为“结束的索引+1”,即下次find()从“4”这个位置开始找。

matcher.group(0):截取str的子串:位置从groups[0]-groups[1]

group(x):x可以为其它参数,当正则中使用到分组时:例如上面的正则换成

1
2
3
4
5
6
7
(\\d\\d)(\\d\\d)

while (matcher.find()){
System.out.println(matcher.group(0));//1919
System.out.println(matcher.group(1));//19
System.out.println(matcher.group(2));//19
}

group()源码:

1
2
3
4
5
6
7
8
9
public String group(int group) {
if (first < 0)
throw new IllegalStateException("No match found");
if (group < 0 || group > groupCount())
throw new IndexOutOfBoundsException("No group " + group);
if ((groups[group*2] == -1) || (groups[group*2+1] == -1))
return null;
return getSubSequence(groups[group * 2], groups[group * 2 + 1]).toString();
}

反向引用

1
2
3
4
5
匹配两个连续的相同数字:(\\d)\\1

匹配五个连续的相同数字:(\\d)\\1{4}

匹配个位与千位相同,十位与百位相同的数:(\\d))(\\d)\\2\\1

结巴去重案例

1
2
3
4
5
6
7
8
9
10
public class Reg03 {
public static void main(String[] args) {
String content = "我我。。。。我。。要学。学学学Java";
String s1 = Pattern.compile("\\。").matcher(content).replaceAll("");
String s2 = Pattern.compile("(.)\\1+").matcher(s1).replaceAll("$1");
System.out.println(content);
System.out.println(s1);
System.out.println(s2);
}
}

String类中使用正则

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public class Reg04 {
public static void main(String[] args) {
//替换
String s = "hello123wo0rld";
System.out.println(s);
s = s.replaceAll("\\d", "");
System.out.println(s);

//分割
String s2 = "2000-03-27 12:00:00";
System.out.println(s2);
String[] split = s2.split("-|:|\\s"); //“\\s”表示空格
for (String s1 : split) {
System.out.println(s1);
}

//匹配
String s3 = "13833330000";
System.out.println(s3.matches("1(38|37)\\d{8}"));
}
}

image-20210616171059331

匹配邮箱

1
2
3
4
5
6
7
8
9
10
public class Reg05 {
public static void main(String[] args) {
String s = "wuguotang@gamil.com";
if(s.matches("^[\\w|_]+@([\\w]+\\.)+[a-z|A-Z]+$")){
System.out.println("yes");
}else{
System.out.println("no");
}
}
}

匹配数字-正、负,整,小数。

1
2
3
4
5
6
7
8
9
10
11
public class Reg05 {
public static void main(String[] args) {
String s = "-0.012";
if(s.matches("^[+-]([1-9]\\d*|0)(.\\d+)?$")){
System.out.println("yes");
}else{
System.out.println("no");
}
}
}

对URL进行解析

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public class Reg05 {
public static void main(String[] args) {
String s = "https://www.fanyi.baidu.com:8080/home/index.html";
Pattern compile = Pattern.compile("^([a-zA-Z]+)://([a-zA-Z.]+):(\\d+)[\\w-/]*/([\\w.]+)$");
Matcher matcher = compile.matcher(s);
if(matcher.find()){
System.out.println(matcher.group(0));
System.out.println(matcher.group(1));
System.out.println(matcher.group(2));
System.out.println(matcher.group(3));
System.out.println(matcher.group(4));

}

}
}

image-20210616175659937

1
Pattern.compile("^hello\\sworld$")

本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!