-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTRegex.java
More file actions
135 lines (118 loc) · 3.73 KB
/
TRegex.java
File metadata and controls
135 lines (118 loc) · 3.73 KB
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package util;
import org.junit.Test;
import java.util.regex.Pattern;
/**
* Created by chenxun on 2017/9/23 22:32
* http://www.runoob.com/java/java-regular-expressions.html
*/
public class TRegex {
@Test
public void method1() {
String content = "I am noob from runoob.com.";
String pattern = ".*runoob.*";
boolean isMatch = Pattern.matches(pattern, content);
System.out.println("字符串中是否包含了 'runoob' 子字符串? " + isMatch);
}
/**
* \s : 应该是space 表示一个空格
* + :表示可以有多个>=1
*/
@Test
public void method3() {
String content = "I am boy";
String pattern = "I\\s+am\\s+boy";
boolean isMatch = Pattern.matches(pattern, content);
System.out.println(isMatch);
}
/**
* 找去其中的数字 第一位为0 去掉 仅有一位是0保留
* todo
*/
@Test
public void method3_1() {
String content = "0sv123SSECve10awev1aewf";
char[] chars = content.toCharArray();
String nums = "";
for (int i = 0; i < chars.length; i++) {
int t = (int)chars[i];
if( 48<=t && t<=57){
nums+=chars[i];
}else {
if(nums.length()>0){
System.out.println(nums);
nums = "";
}
}
}
}
/**
* 过滤其中的字母
* 过滤其中的字符
*/
@Test
public void method3_2() {
String content = "sv123SSECve10awev1aewf00";
String pattern = "\\d+";
System.out.println(content.replaceAll("[(0-9)]","").toString());//svSSECveawevaewf
System.out.println(content.replaceAll("[^0-9]","").toString());//12310100 替换非数字
}
/**
* 去除所有空格
*/
@Test
public void method3_3() {
String content = " I am boy ";
System.out.println(content.replaceAll("\\s","").toString());
}
/**
* ^ :以什么开始
* \d+: 一个或多个数字
* ? 表示可选
* \. 匹配 .
* (pattern) :匹配pattern 这个子表达式 [] 里面是字符 数字等 也可以是表达式 其实也是特殊的表达式
*/
@Test
public void method4() {
String content = "5.5";
String pattern = "^\\d+(\\.\\d+)?";
boolean isMatch = Pattern.matches(pattern, content);
System.out.println(isMatch);
}
/**
* 其他的语言中,一个反斜杠\就足以具有转义的作用,
* 而在正则表达式中则需要有两个反斜杠才能被解析为其他语言中的转义作用
*
* \ \ \\
* 转义符 本身就得2个存在
* 好比空格正则匹配:
* \ \ s
*/
@Test
public void method5() {
String content = "\\";
String pattern = "\\\\";
boolean isMatch = Pattern.matches(pattern, content);
System.out.println(isMatch);
}
@Test
public void method6() {
String content = "SLO_20190614115325.xls";
String pattern = "^SLO_\\d{14}.xls";
boolean isMatch = Pattern.matches(pattern, content);
System.out.println(isMatch);
}
@Test
public void method7() {
String content2 = "xx细表-308010700992381_20090521.csv";
String pattern2 = "^xx细表-\\d{15}_\\d{8}.csv";
boolean isMatch2 = Pattern.matches(pattern2, content2);
System.out.println(isMatch2);
}
@Test
public void method8() {
String content2 = "310010,7009,92381";
String pattern2 = "^\\d(\\d|\\,)*\\d$";
boolean isMatch2 = Pattern.matches(pattern2, content2);
System.out.println(isMatch2);
}
}