最近写的一个正则表达式验证器,很简单!很简单!

前端之家收集整理的这篇文章主要介绍了最近写的一个正则表达式验证器,很简单!很简单!前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
package com.freud.regex;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;

/**
 * 
 * @title 0150 ( Small Tool ) - Regex_Validate_tool
 * @author Freud,Kang
 * 
 */
public class Validater {

	/** The main frame */
	private JFrame frame;
	/** The background panel */
	private JPanel background;

	/** The Label to show "regex" */
	private JLabel regex_Label;
	/** The TextArea to input regex expression */
	private JTextArea regex_TextArea;
	/** The Label to show "source" */
	private JLabel source_label;
	/** The TextArea to input source string */
	private JTextArea source_TextArea;

	/** The submit button */
	private JButton submit;
	/** The result Label to show result message */
	private JLabel result;
	/** The result Panel to show Color status */
	private JPanel result_panel;

	/**
	 * Constructor
	 */
	private Validater() {

		/**
		 * init main frame
		 */
		frame = new JFrame("Regex Validator");
		frame.setBounds(100,100,400,500);
		frame.setVisible(true);
		frame.setLayout(null);
		frame.setDefaultCloSEOperation(JFrame.EXIT_ON_CLOSE);

		/**
		 * init main panel
		 */
		background = new JPanel();
		background.setSize(400,500);
		background.setLayout(null);
		background.setBackground(new Color(80,188,252));

		/**
		 * init components in main panel
		 */
		regex_Label = new JLabel("Regex");
		regex_Label.setBounds(10,10,50,20);

		regex_TextArea = new JTextArea();
		regex_TextArea.setBounds(60,300,100);

		source_label = new JLabel("Source");
		source_label.setBounds(10,130,10);

		source_TextArea = new JTextArea();
		source_TextArea.setBounds(60,100);

		submit = new JButton("Submit");
		submit.setBounds(150,250,30);
		submit.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				String source = source_TextArea.getText();
				String regex = regex_TextArea.getText();

				if (regex == null || regex.isEmpty()) {
					notEmpty("Regex cannot be empty!");
					return;
				}

				reset(exe(source,regex));
			}
		});

		/**
		 * init result panel
		 */
		result_panel = new JPanel();
		result_panel.setBounds(10,360,20);
		result_panel.setBackground(Color.WHITE);
		result_panel.setLayout(null);

		result = new JLabel(" Result  :  Waiting");
		result.setBounds(140,110,20);
		result.setBackground(Color.WHITE);

		/**
		 * add the components to correct parents components
		 */
		result_panel.add(result);

		background.add(regex_Label);
		background.add(regex_TextArea);
		background.add(source_label);
		background.add(source_TextArea);
		background.add(submit);
		background.add(result_panel);

		frame.add(background);

		// repaint
		frame.repaint();
	}

	/**
	 * Use java API to judge whether source and regex matched
	 * 
	 * @param source
	 *            source string
	 * @param regex
	 *            regex expression
	 * @return
	 */
	private boolean exe(String source,String regex) {
		try {

			Pattern pattern = Pattern.compile(regex);
			Matcher matcher = pattern.matcher(source);

			boolean result = matcher.matches();
			String path = Validater.class.getProtectionDomain().getCodeSource().getLocation().toString().replace("file:/","").replace("file:\\","")
					.replace("bin\\","").replace("bin/","");

			BufferedWriter bw = null;

			try {

				bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File(path + "Regex_validated_Log.txt"),true)));

				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				String date = sdf.format(new Date());

				bw.append(date);
				bw.append("\r\n\t\t\tregex : ");
				bw.append(regex);
				bw.append("\r\n\t\t\tSource : ");
				bw.append(source);
				bw.append("\r\n\t\t\tResult : ");
				bw.append(String.valueOf(result));
				bw.append("\r\n\r\n");

				bw.flush();
			} finally {
				if (bw != null) {
					bw.flush();
					bw.close();
				}
			}

			return result;

		} catch (Exception e) {
			return false;
		}

	}

	/**
	 * Main method
	 * 
	 * @param args
	 */
	public static void main(String[] args) {
		new Validater();
	}

	/**
	 * According to the compared result,reset the result components
	 * 
	 * @param flag
	 *            Type : Boolean
	 */
	private void reset(boolean flag) {
		/**
		 * if the result is true means the regex matches the source string
		 */
		if (flag) {
			result_panel.setBackground(Color.GREEN);
			result.setBackground(Color.GREEN);
			result.setText(" Result  :  success");

			/**
			 * Otherwise means not matched
			 */
		} else {
			result_panel.setBackground(Color.RED);
			result.setBackground(Color.RED);
			result.setText(" Result  :  Failed");
		}

		// repaint
		frame.repaint();
	}

	private void notEmpty(String error) {
		result.setBounds(120,150,20);
		result.setText(error);
		result_panel.setBackground(Color.PINK);
		result.setBackground(Color.PINK);
		frame.repaint();
	}
}


最近写的一个正则表达式验证器,很简单,很简单。不过很好玩!

没有什么技术含量,贴出来玩玩吧!

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

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