起因

由于刚开始学习Java,很多语法都不会,有一次做到一道题,仔细思考过后发现可以用高中学的visual basic中的Mid()函数,但是我无法用Java语言给他表示出来/(ㄒoㄒ)/~~,下面是我想出来的“伪代码”。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import java.util.Scanner;

public class Mid_Java {
public static String Mid(String in, int i, int j){
char a[] = in.toCharArray();
String result = "";
for(int x = i -1;x < j;x++){
result = result + a[x];
}
return result;
}

public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String a = scanner.nextLine();
int i = scanner.nextInt();
int j = scanner.nextInt();
System.out.println(Mid(a,i,j));
}
}

学习

后来通过学长,知道了还有正则表达式这样一个东西

正则表达式(简略版)

概述

正则表达式简单说来就是通过一串字符来描述一个规则,我们可以用这个规则来和输入值进行匹配,从而输出true或者false。
正则表达式可以大幅度简化我们的代码长度,但是他需要一定的学习成本(因为要记的东西实在有点多/(ㄒoㄒ)/~~),但是你只要理解并多使用它,你会发现它真的非常好用!

构成

正则表达式由普通字符和元字符构成,普通字符就不说了,所谓的元字符就是在正则表达式中能表达特定意思的一类字符。

Java中的正则校验

正则表达式在Java中的主要用途就是用于校验字符串,格式如下:

1
boolean result = Pattern.matches(regex,input);

regex表示规则
input表示输入的字符串

一些正则表达式

注意:在Java的正则表达式中,“/”要写成“//”,因为前面的一个“/”表示字符串转义!!!

1、普通字符

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import java.util.Scanner;
import java.util.regex.Pattern;

public class regex {
public static boolean check(String str){
boolean flag = Pattern.matches("abcdef",str);
return flag;
}

public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("abcdef");
String a = scanner.nextLine();
boolean flag = check(a);
System.out.println(flag);
}
}

2、\d

\d表示一个数字
如:aaa\b表示aaa后跟着一个数字
aaa\dbb表示aaa和bb中间有一个数字

3、\D

\D与\d刚好相反,表示一个非数字

4、\w

\w表示一个字母、数字或者下划线,字母大小写都可以

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import java.util.regex.Pattern;

public class regex {
public static boolean check(String str){
boolean flag = Pattern.matches("12\\w45",str);
return flag;
}

public static void main(String[] args) {
String a1 = "12345";
String a2 = "12S45";
String a3 = "12s45";
String a4 = "12_45";
String a5 = "12=45";
String a6 = "12 45";
System.out.println(a1+check(a1));
System.out.println(a2+check(a2));
System.out.println(a3+check(a3));
System.out.println(a4+check(a4));
System.out.println(a5+check(a5));
System.out.println(a6+check(a6));
}
}

输出:

5、\W

\W与\w相反

6、\s

\s表示一个看不见的字符,即空格或Tab

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import java.util.regex.Pattern;

public class regex {
public static boolean check(String str){
boolean flag = Pattern.matches("12\\s45",str);
return flag;
}

public static void main(String[] args) {
String a1 = "12345";
String a2 = "12S45";
String a3 = "12s45";
String a4 = "12_45";
String a5 = "12=45";
String a6 = "12 45";
System.out.println(a1+check(a1));
System.out.println(a2+check(a2));
System.out.println(a3+check(a3));
System.out.println(a4+check(a4));
System.out.println(a5+check(a5));
System.out.println(a6+check(a6));
}
}

输出:

7、\S

\S与\s相反,表示一个看得见的字符

8、.

.(小数点)表示任意单个字符,除了“\n”和“\r”

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import java.util.regex.Pattern;

public class regex {
public static boolean check(String str){
boolean flag = Pattern.matches("12...67",str);
return flag;
}

public static void main(String[] args) {
String a1 = "1234567";
String a2 = "12S4567";
String a3 = "12s4567";
String a4 = "12_4567";
String a5 = "12=4567";
String a6 = "12 4 67";
System.out.println(a1+check(a1));
System.out.println(a2+check(a2));
System.out.println(a3+check(a3));
System.out.println(a4+check(a4));
System.out.println(a5+check(a5));
System.out.println(a6+check(a6));
}
}

输出:

9、|

|表示或的关系,即输入的字符串对应正则表达式中的一个就行

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import java.util.regex.Pattern;

public class regex {
public static boolean check(String str){
boolean flag = Pattern.matches("12|34|56",str);
return flag;
}

public static void main(String[] args) {
String a1 = "123456";
String a2 = "12";
String a3 = "34";
String a4 = "56";
//String a5 = "";
//String a6 = "";
System.out.println(a1+check(a1));
System.out.println(a2+check(a2));
System.out.println(a3+check(a3));
System.out.println(a4+check(a4));
//System.out.println(a5+check(a5));
//System.out.println(a6+check(a6));
}
}

输出:

当你需要选择的字符串前后有字符串时,一定要加上括号
如:aa(bb|cc|dd)ee

10、[]

[]表示匹配中括号内的任意一个字符

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import java.util.regex.Pattern;

public class regex {
public static boolean check(String str){
boolean flag = Pattern.matches("[abc]",str);
return flag;
}

public static void main(String[] args) {
String a1 = "a";
String a2 = "ab";
String a3 = "b";
String a4 = "c";
String a5 = "d";
//String a6 = "";
System.out.println(a1+" "+check(a1));
System.out.println(a2+" "+check(a2));
System.out.println(a3+" "+check(a3));
System.out.println(a4+" "+check(a4));
System.out.println(a5+" "+check(a5));
//System.out.println(a6+check(a6));
}
}

输出:

11、[^]

[^]表示与括号中的字符都不匹配

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import java.util.regex.Pattern;

public class regex {
public static boolean check(String str){
boolean flag = Pattern.matches("[^abc]",str);
return flag;
}

public static void main(String[] args) {
String a1 = "a";
String a2 = "ab";
String a3 = "b";
String a4 = "c";
String a5 = "d";
//String a6 = "";
System.out.println(a1+" "+check(a1));
System.out.println(a2+" "+check(a2));
System.out.println(a3+" "+check(a3));
System.out.println(a4+" "+check(a4));
System.out.println(a5+" "+check(a5));
//System.out.println(a6+check(a6));
}
}

输出:

12、[a-z]

[a-z]表示的是a到z中的任意一个字符,当然,a和z可以替换成别的字符,[a-z]这个表达式可以用来检测字符串中是否有字母

13、[^a-z]

不想说了,原理一样

14、\num

这里的num是数字
举个例子:正则表达式:ab(c)de\1
这个表示只有abcde能和他匹配

正则表达式:ab(c)(d)e\2
匹配式:abcded

正则表达式:ab(c)(d)e\1\2
匹配式:abcdecd

15、?

?表示前面一个字符能出现一次或零次

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public class regex {
public static boolean check(String str){
boolean flag = Pattern.matches("abc?de",str);
return flag;
}

public static void main(String[] args) {
String a1 = "abcde";
String a2 = "abccde";
String a3 = "abde";
//String a4 = "c";
//String a5 = "d";
//String a6 = "";
System.out.println(a1+" "+check(a1));
System.out.println(a2+" "+check(a2));
System.out.println(a3+" "+check(a3));
//System.out.println(a4+" "+check(a4));
//System.out.println(a5+" "+check(a5));
//System.out.println(a6+check(a6));
}
}

输出:

16、+

+表示前面的字符至少要出现一次

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import java.util.regex.Pattern;

public class regex {
public static boolean check(String str){
boolean flag = Pattern.matches("abc+de",str);
return flag;
}

public static void main(String[] args) {
String a1 = "abcde";
String a2 = "abcccccccde";
String a3 = "abde";
//String a4 = "c";
//String a5 = "d";
//String a6 = "";
System.out.println(a1+" "+check(a1));
System.out.println(a2+" "+check(a2));
System.out.println(a3+" "+check(a3));
//System.out.println(a4+" "+check(a4));
//System.out.println(a5+" "+check(a5));
//System.out.println(a6+check(a6));
}
}

输出:

17、{n}

{n}表示前面的字符出现了n次

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import java.util.regex.Pattern;

public class regex {
public static boolean check(String str){
boolean flag = Pattern.matches("abc{3}de",str);
return flag;
}

public static void main(String[] args) {
String a1 = "abcde";
String a2 = "abcccde";
String a3 = "abc cde";
//String a4 = "c";
//String a5 = "d";
//String a6 = "";
System.out.println(a1+" "+check(a1));
System.out.println(a2+" "+check(a2));
System.out.println(a3+" "+check(a3));
//System.out.println(a4+" "+check(a4));
//System.out.println(a5+" "+check(a5));
//System.out.println(a6+check(a6));
}
}

输出:

18、{n,m}

{n,m}表示前面的字符至少出现了n次,至多出现了m次

19、*

*表示前面的字符出现了任意次数,包括0次