最近写的一个正则表达式验证器,很简单!很简单!
2013-04-15 16:53
369 查看
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, 10, 300, 100); source_label = new JLabel("Source"); source_label.setBounds(10, 130, 50, 10); source_TextArea = new JTextArea(); source_TextArea.setBounds(60, 130, 300, 100); submit = new JButton("Submit"); submit.setBounds(150, 250, 100, 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, 300, 360, 20); result_panel.setBackground(Color.WHITE); result_panel.setLayout(null); result = new JLabel(" Result : Waiting"); result.setBounds(140, 0, 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, 0, 150, 20); result.setText(error); result_panel.setBackground(Color.PINK); result.setBackground(Color.PINK); frame.repaint(); } }
最近写的一个正则表达式验证器,很简单,很简单。不过很好玩!
没有什么技术含量,贴出来玩玩吧!
相关文章推荐
- [VB.NET]谁能帮我用验证国内电话号码的正则表达式写一个验证程序啊?
- 一个验证日期格式的超强正则表达式
- C#正则表达式基础 [a-z] 验证一个字符是否是小写字母
- 输入一个Email地址,然后使用正则表达式验证该Email地址是否正确。
- C#正则表达式基础 [A-Z]验证一个字符是否是大写字母
- 用JavaScript的正则表达式验证网址格式是否正确,方法很多,这只是一个实现的方法.
- 一个正则表达式验证密码强度
- 一个能验证日期的正则表达式
- 一个正则表达式验证密码强度
- 输入一个Email地址,然后使用正则表达式验证该Email地址是否正确。
- 输入一个Email地址,然后使用正则表达式验证该Email地址是否正确。
- 一个正则表达式验证密码强度
- C#正则表达式基础 [0-9a-zA-Z] 验证一个字符是否是数字或者字母
- 输入一个Email地址,然后使用正则表达式验证该Email地址是否正确。
- 一个验证用户名的正则表达式
- js自己写的一个手机电话同时可以验证的正则表达式
- 输入一个Email地址,然后使用正则表达式验证该Email地址是否正确。
- js中如何验证一个字符是否为大写字母,不用正则表达式
- [转]一个验证日期格式的超强正则表达式
- 一个正则表达式验证密码强度