习题,正则表达式

前端之家收集整理的这篇文章主要介绍了习题,正则表达式前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

1、主要是验证用户界面输入信息是否合法

效果


很简单,熟悉基本的正则表达式用法就可,大部分时间都花在界面上了。

源码连接:http://download.csdn.net/detail/anlidengshiwei/8423187,地址点击打开链接

/*
要求:用Java代码做以下正则匹配,模拟用户注册时,验证填写信息的准确性
1、匹配邮箱
2、匹配手机号
3、匹配密码(6~12位之间,不能为纯数字)
4、匹配IP(IPv4)

题目分析:
1、匹配邮箱
总体标准:6~18个字符,可使用字母、数字、下划线,需以字母开头
个别邮箱长度不同,不作考虑。
*/
class RegexTools
{
	/*
	1、匹配邮箱
	总体标准:6~18个字符,可使用字母、数字、下划线,需以字母开头
	个别邮箱长度不同,不作考虑。
	*/
	public static boolean checkMail(String str)
	{
		if(str == null)return false;
		String regex = "^[a-zA-Z](\\w|_){5,17}@\\w+\\.?\\w{2,3}$";
		return str.matches(regex);
	}

	/*
	2、匹配手机号
	130-132、186是联通的,133和153、189是电信的,134-139、159、187、188是移动的
	按照这个标准:
	第一位:1
	第二位:3、5、8
	总长度:11位
	*/
	public static boolean checkTelphone(String str)
	{
		if(str == null)return false;
		String regex = "^1[358]\\d{9}$";
		return str.matches(regex);
	}

	/*
	匹配密码,不能为纯数字,那么我们就采用取反的思路来做
	*/
	public static boolean checkPassWord(String str)
	{
		if(str == null)return false;
		String regex = "^(\\d){6,12}$";
		return !str.matches(regex);
	}

	/*
	4、匹配ip
	IP地址的长度为32位,分为4段,每段8位,用十进制数字表示,每段数字范围为0~255,段与段之间用
	英文句点“.”隔开。
	注意点:
	1、前三段有.分割,最后一段没有
	2、0-255的表示
	*/
	public static boolean checkIP4(String str)
	{
		if(str == null)return false;
		String regex = "((2[0-4]\\d|25[0-5]|[01]?\\d?\\d)\\.){3}(2[0-4]\\d|25[0-5]|[01]?\\d?\\d)$";
		return str.matches(regex);
	}
}

界面类:
import java.awt.Frame;
import java.awt.Button;
import java.awt.Label;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;  
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Graphics;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.TextField;
import javax.swing.JOptionPane; 

class RegisterFrameDemo
{
	public static void main(String[] args) 
	{
		final RegisterFrame frame = new RegisterFrame();
		ButtonClass btn = new ButtonClass(frame);
		btn.create();
		((Button)frame.getComponent(10)).addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e)
			{
				String sName = ((TextField)frame.getComponent(5)).getText();
				String sPwd = ((TextField)frame.getComponent(6)).getText();
				String sMail = ((TextField)frame.getComponent(7)).getText();
				String sTel = ((TextField)frame.getComponent(8)).getText();
				String sIp = ((TextField)frame.getComponent(9)).getText();
				JOptionPane.showMessageDialog(frame,sName + "信息中检测结果如下:\n" + 
							"密码是否合法:" + RegexTools.checkPassWord(sPwd) + "\n" +
							"邮箱是否合法:" + RegexTools.checkMail(sMail) + "\n" +
							"电话是否合法:" + RegexTools.checkTelphone(sTel) + "\n" +
							"IP是否合法:" + RegexTools.checkIP4(sIp) + "\n");
			}
		});
		((Button)frame.getComponent(11)).addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e)
			{
				((TextField)frame.getComponent(5)).setText("");
				((TextField)frame.getComponent(6)).setText("");
				((TextField)frame.getComponent(7)).setText("");
				((TextField)frame.getComponent(8)).setText("");
				((TextField)frame.getComponent(9)).setText("");
			}
		});
	}
}

class RegisterFrame extends Frame
{
	public RegisterFrame()
	{
		setTitle("用户注册");
		setVisible(true); 
		Toolkit tl = Toolkit.getDefaultToolkit();
		Dimension screenSize = tl.getScreenSize();
		int width = screenSize.width;
		int height =screenSize.height;
		setSize(289,256);
		setLocation(width/4,height/4);
		this.addWindowListener(new FrameEvent());
		this.setLayout(null);
		this.setResizable(false);
		this.repaint();
	}
}

class FrameEvent extends WindowAdapter
{
	public void windowClosing(WindowEvent we)
	{
		System.exit(0);   
	}
}
class ButtonClass
{
	private Frame frame;
	public ButtonClass(Frame frame)
	{
		this.frame = frame;
	}

	public void create()
	{
		//创建控件
		Label lblName = new Label("姓名:");
		Label lblPwd = new Label("密码:");
		Label lblMail = new Label("邮箱:");
		Label lblTel = new Label("手机号码:");
		Label lblIp = new Label("IP4地址:");
		TextField txtName = new TextField();
		TextField txtPwd = new TextField();
		TextField txtMail = new TextField();
		TextField txtTel = new TextField();
		TextField txtIp = new TextField();
		Button btnOk = new Button("确定");
		Button btnCancel = new Button("重置");

		//设置属性
		lblName.setSize(53,12);
		lblPwd.setSize(53,12);
		lblMail.setSize(53,12);
		lblTel.setSize(53,12);
		lblIp.setSize(53,12);
		txtName.setSize(157,21);
		txtPwd.setSize(157,21);
		txtMail.setSize(157,21);
		txtTel.setSize(157,21);
		txtIp.setSize(157,21);
		btnOk.setSize(75,23);
		btnCancel.setSize(75,23);

		lblName.setLocation(35,47);
		lblPwd.setLocation(37,81);
		lblMail.setLocation(37,112);
		lblTel.setLocation(13,147);
		lblIp.setLocation(19,176);
		txtName.setLocation(93,43);
		txtPwd.setLocation(93,75);
		txtMail.setLocation(93,107);
		txtTel.setLocation(93,139);
		txtIp.setLocation(93,171);
		btnOk.setLocation(93,209);
		btnCancel.setLocation(175,208);


		//添加控件
		this.frame.add(lblName);
		this.frame.add(lblPwd);
		this.frame.add(lblMail);
		this.frame.add(lblTel);
		this.frame.add(lblIp);
		this.frame.add(txtName);
		this.frame.add(txtPwd);
		this.frame.add(txtMail);
		this.frame.add(txtTel);
		this.frame.add(txtIp);
		this.frame.add(btnOk);
		this.frame.add(btnCancel);
	}
}


补充知识:

对话框提示

你可以使用JOptionPane,它提供了showMessageDialog、showInputDialog、showOptionDialog、 showConfirmDialog来实现相应的各种功能的对话框。

原文链接:https://www.f2er.com/regex/360769.html

猜你在找的正则表达式相关文章