Java实验七

《Java程序设计》课程第七周实验题目和代码。

实验内容:正则表达式应用

编写一个程序,训练使用正则表达式。具体要求如下:

  1. 创建一个类 RegexChk,作为提供各种方法,实现对整数、手机号码、电子邮
    箱、邮政编码、身份证号码的有效性验证。 2) 要求:
    验证整数时,能够同时校验正整数、负整数和 0;
    验证电子邮箱时,用户名大于等于 3 个字符,@后域名由 1 个或 2 个点组成;
    验证邮政编码时,必须满足 6 个数字长度;
    验证身份证号码时,长度为 15 或 18 位; 3) 使用 Pattern 类和 Matcher 类实现,附使用核心代码: Pattern pattern = Pattern.compile(reg); Matcher matcher = pattern.matcher(string); result = matcher.matches(); 其中,reg 为正则表达式,string 为待验证字符串对象。
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

//编写一个程序,训练使用正则表达式。具体要求如下:
//1) 创建一个类 RegexChk,作为提供各种方法,实现对整数、手机号码、电子邮箱、邮政编码、身份证号码的有效性验证。
//2) 要求:
//验证整数时,能够同时校验正整数、负整数和 0;
//验证电子邮箱时,用户名大于等于 3 个字符,@后域名由 1 个或 2 个点组成;
//验证邮政编码时,必须满足 6 个数字长度;
//验证身份证号码时,长度为 15 或 18 位;
//3) 使用 Pattern 类和 Matcher 类实现,附使用核心代码:
//Pattern pattern = Pattern.compile(reg);
//Matcher matcher = pattern.matcher(string);
//result = matcher.matches();
//其中,reg 为正则表达式,string 为待验证字符串对象。

import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class RegexTest {
public static void main (String args[ ]) {
System.out.println("请输入待检测数据:");
Scanner scanner = new Scanner(System.in);
String str = scanner.nextLine();
RegexChk t;
t= new RegexChk(str);
t.integer();
t.email();
t.postCode();
t.idNumber();

}
}

//创建类 RegexChk,作为提供各种方法,实现对整数、手机号码、电子邮箱、邮政编码、身份证号码的有效性验证。
class RegexChk{
private String content;

public RegexChk(){
content="";
}

public RegexChk(String content){
this.content=content;
}


void integer() {
//定义判别正整数的正则表达式
String pInt="^[1-9][0-9]*$";
//定义判别负整数的正则表达式
String nInt="^-[1-9][0-9]*$";
//定义判别0的正则表达式
String zero="^0$";
Pattern pp=Pattern.compile(pInt);
Matcher mp=pp.matcher(content);
boolean result=mp.matches();
//System.out.println(result);
if(result) {
System.out.println(content+"是正整数");
}
else {
Pattern pn=Pattern.compile(nInt);
Matcher mn=pn.matcher(content);
//System.out.println(mn.matches());

if(mn.matches()) {
System.out.println(content+"是负整数");
}
else {
Pattern pz=Pattern.compile(zero);
Matcher mz=pz.matcher(content);
//System.out.println(mz.matches());

if(mz.matches()) {
System.out.println(content+"是零");
}
else {
System.out.println(content+"不是整数");
}
}

}
}

void email() {
//定义判别电子邮箱的正则表达式(,用户名大于等于 3 个字符,@后域名由 1 个或 2 个点组成)
String reg="^[A-Za-z0-9-._]{3,}+@[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)?(\\.[A-Za-z])$";
Pattern p=Pattern.compile(reg);
Matcher m=p.matcher(content);
//System.out.println(m.matches());
if(m.matches()) {
System.out.println(content+"是邮箱");
}
else {
System.out.println(content+"不是邮箱");
}

}

void postCode() {
//定义判别邮政编码的正则表达式(6 个数字长度)
String reg="^[0-9]{6}$";
Pattern p=Pattern.compile(reg);
Matcher m=p.matcher(content);
if(m.matches()) {
System.out.println(content+"是邮政编码");
}
else {
System.out.println(content+"不是邮政编码");
}

}

void idNumber() {
// 定义判别用户身份证号的正则表达式(15位或者18位,最后一位可以为字母)
String reg="(^[1-9]\\d{5}(18|19|20)\\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\\d{3}[0-9Xx]$)|" +
"(^[1-9]\\d{5}\\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\\d{3}$)";
Pattern p=Pattern.compile(reg);
Matcher m=p.matcher(content);
if(m.matches()) {
System.out.println(content+"是身份证号码");
}
else {
System.out.println(content+"不是身份证号码");
}

}

}

打赏
  • © 2025 Aoxue
  • Hexo Theme Ayer by shenyu
    • PV:
    • UV:

请我喝杯咖啡吧~

支付宝
微信