Skip to content

Commit 6c42e46

Browse files
正则表达式示例
1 parent a52dc30 commit 6c42e46

File tree

6 files changed

+131
-15
lines changed

6 files changed

+131
-15
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.regex;
2+
3+
import java.util.regex.Matcher;
4+
import java.util.regex.Pattern;
5+
6+
public class PatternDemo2 {
7+
public static void main(String args[]){
8+
String str="1983-07-27";
9+
String pat="\\d{4}-\\d{2}-\\d{2}";
10+
Pattern p=Pattern.compile(pat); //先声明模式
11+
Matcher m=p.matcher(str); //接着再匹配
12+
if(m.matches()) {
13+
System.out.println("日期格式各法");
14+
}else {
15+
System.out.println("日期格式不合法");
16+
17+
}
18+
19+
20+
}
21+
}
Lines changed: 50 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,57 @@
11
package com.regex;
22

3-
import java.util.regex.Matcher;
4-
import java.util.regex.Pattern;
5-
3+
/**
4+
* @Author: lenovo
5+
* @Date: 2020/2/2 21:46
6+
* @Description:
7+
*
8+
* 详情参见:https://www.cnblogs.com/expiator/p/12250598.html
9+
*/
610
public class RegexDemo {
7-
public static void main(String args[]){
8-
String str="1983-07-27";
9-
String pat="\\d{4}-\\d{2}-\\d{2}";
10-
Pattern p=Pattern.compile(pat); //先声明模式
11-
Matcher m=p.matcher(str); //接着再匹配
12-
if(m.matches()) {
13-
System.out.println("日期格式各法");
14-
}else {
15-
System.out.println("日期格式不合法");
1611

17-
}
12+
public static void regex1(){
13+
String str="abcdddabc";
14+
String regex=".*d+.*";
15+
if (str.matches(regex)){
16+
System.out.println(true);
17+
}
18+
}
19+
20+
public static void regex2(){
21+
String str="abcdefg";
22+
String regex=".*efg";
23+
if (str.matches(regex)){
24+
System.out.println(true);
25+
}
26+
}
27+
28+
29+
public static void regex3(){
30+
String str="a";
31+
String regex="\\w";
32+
if (str.matches(regex)) {
33+
System.out.println(true);
34+
}
35+
}
36+
37+
public static void regex4(){
38+
String str="abdf1459";
39+
String regex="[a-z]{4}[1-9]{4}";
40+
if (str.matches(regex)) {
41+
System.out.println(true);
42+
}
43+
}
44+
1845

46+
public static void regex6(){
47+
String str="catdog";
48+
String regex="^cat$";
49+
String regex2="^cat.*";
50+
String regex3=".*dog$";
51+
boolean isMatch1=str.matches(regex);
52+
boolean isMatch2=str.matches(regex2);
53+
boolean isMatch3=str.matches(regex3);
54+
System.out.println(isMatch1+","+isMatch2+","+isMatch3);
55+
}
1956

20-
}
2157
}

src/main/java/com/regex/RegexTest.java renamed to src/main/java/com/regex/RegexLetterTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import java.util.regex.Matcher;
44
import java.util.regex.Pattern;
55

6-
public class RegexTest {
6+
public class RegexLetterTest {
77
public static void main(String args[]){
88
//输入字符串,然后验证是否符合格式
99
String str="QQabc4755";

src/main/java/com/string/CharSwapDemo.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.string;
22

33
import com.alibaba.druid.sql.visitor.functions.Char;
4+
import org.apache.commons.lang.StringUtils;
45

56
/**
67
* @Author: lenovo
@@ -22,4 +23,11 @@ public static String swapCharInString(String str) {
2223
return sb.toString();
2324
}
2425

26+
public static String reveserString() {
27+
String str="abcd";
28+
String str2 = StringUtils.reverse(str);
29+
System.out.println(str2);
30+
return str2;
31+
}
32+
2533
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.regex;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import static org.junit.jupiter.api.Assertions.*;
6+
7+
/**
8+
* @Author: lenovo
9+
* @Date: 2020/2/2 21:54
10+
* @Description:
11+
*/
12+
class RegexDemoTest {
13+
14+
@Test
15+
void regex1() {
16+
RegexDemo.regex1();
17+
}
18+
19+
20+
@Test
21+
void regex2() {
22+
RegexDemo.regex2();
23+
}
24+
25+
26+
27+
28+
@Test
29+
void regex3() {
30+
RegexDemo.regex3();
31+
}
32+
33+
@Test
34+
void regex4() {
35+
RegexDemo.regex4();
36+
}
37+
38+
39+
@Test
40+
void regex6() {
41+
RegexDemo.regex6();
42+
}
43+
44+
45+
}

src/test/java/com/string/CharSwapDemoTest.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,10 @@ void swapCharInString() {
1616
String s = CharSwapDemo.swapCharInString("abcd");
1717
System.out.println(s);
1818
}
19+
20+
@Test
21+
void reveserString() {
22+
CharSwapDemo.reveserString();
23+
24+
}
1925
}

0 commit comments

Comments
 (0)