利用java实现下内容 验证正则表达式 设计一格正则表达式验证程序,能够验证用户名、密码、邮箱、手机号码、日期、时间、网址、身份证 号等,将历史验证信息保存在out.txt。 应该包括下面几个知识点的应用:类的定义、GUI界面设计、文件IO、集合;缺少功能扣分
时间: 2023-07-04 12:21:04 浏览: 121
好的,下面是一个简单的 Java 实现,可以验证用户名、密码、邮箱、手机号码、日期、时间、网址、身份证号等,并将历史验证信息保存在 out.txt 文件中。
```java
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.text.*;
import java.util.*;
import java.util.regex.*;
import javax.swing.*;
public class RegexValidator implements ActionListener {
private JFrame frame;
private JTextField usernameField, passwordField, emailField, phoneField, dateField, timeField, urlField, idField;
private JTextArea historyArea;
private JButton validateButton;
public RegexValidator() {
// 创建 GUI 界面
frame = new JFrame("Regex Validator");
JPanel panel = new JPanel(new GridLayout(0, 2));
panel.add(new JLabel("Username:"));
usernameField = new JTextField(20);
panel.add(usernameField);
panel.add(new JLabel("Password:"));
passwordField = new JTextField(20);
panel.add(passwordField);
panel.add(new JLabel("Email:"));
emailField = new JTextField(20);
panel.add(emailField);
panel.add(new JLabel("Phone:"));
phoneField = new JTextField(20);
panel.add(phoneField);
panel.add(new JLabel("Date:"));
dateField = new JTextField(20);
panel.add(dateField);
panel.add(new JLabel("Time:"));
timeField = new JTextField(20);
panel.add(timeField);
panel.add(new JLabel("URL:"));
urlField = new JTextField(20);
panel.add(urlField);
panel.add(new JLabel("ID:"));
idField = new JTextField(20);
panel.add(idField);
validateButton = new JButton("Validate");
validateButton.addActionListener(this);
panel.add(validateButton);
frame.getContentPane().add(panel, BorderLayout.NORTH);
historyArea = new JTextArea(10, 40);
historyArea.setEditable(false);
frame.getContentPane().add(new JScrollPane(historyArea), BorderLayout.CENTER);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
// 获取文本框输入的值
String username = usernameField.getText();
String password = passwordField.getText();
String email = emailField.getText();
String phone = phoneField.getText();
String date = dateField.getText();
String time = timeField.getText();
String url = urlField.getText();
String id = idField.getText();
// 定义正则表达式
String usernameRegex = "^[a-zA-Z0-9_]{4,16}$";
String passwordRegex = "^[a-zA-Z0-9_]{6,18}$";
String emailRegex = "^\\w+@\\w+\\.\\w+$";
String phoneRegex = "^1[3-9]\\d{9}$";
String dateRegex = "^\\d{4}-\\d{2}-\\d{2}$";
String timeRegex = "^\\d{2}:\\d{2}:\\d{2}$";
String urlRegex = "^https?://\\w+\\.\\w+$";
String idRegex = "^\\d{17}[0-9X]$";
// 验证输入值是否符合正则表达式
ArrayList<String> result = new ArrayList<String>();
if (!username.matches(usernameRegex)) {
result.add("Invalid username");
}
if (!password.matches(passwordRegex)) {
result.add("Invalid password");
}
if (!email.matches(emailRegex)) {
result.add("Invalid email");
}
if (!phone.matches(phoneRegex)) {
result.add("Invalid phone");
}
if (!date.matches(dateRegex)) {
result.add("Invalid date");
}
if (!time.matches(timeRegex)) {
result.add("Invalid time");
}
if (!url.matches(urlRegex)) {
result.add("Invalid URL");
}
if (!id.matches(idRegex)) {
result.add("Invalid ID");
}
// 将验证结果保存到文件中
try {
FileWriter writer = new FileWriter("out.txt", true);
writer.write(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()) + "\n");
if (result.isEmpty()) {
writer.write("All inputs are valid\n");
} else {
for (String s : result) {
writer.write(s + "\n");
}
}
writer.close();
} catch (IOException ex) {
ex.printStackTrace();
}
// 更新历史验证信息
try {
FileReader reader = new FileReader("out.txt");
BufferedReader bufferedReader = new BufferedReader(reader);
String line;
StringBuilder stringBuilder = new StringBuilder();
while ((line = bufferedReader.readLine()) != null) {
stringBuilder.append(line).append("\n");
}
bufferedReader.close();
historyArea.setText(stringBuilder.toString());
} catch (IOException ex) {
ex.printStackTrace();
}
}
public static void main(String[] args) {
new RegexValidator();
}
}
```
这个程序包括了类的定义、GUI 界面设计、文件 IO、集合等知识点的应用。如果需要增加其他功能,可以在这个基础上进行扩展。
阅读全文