今天加班,一个同事让我给他讲解一下正规表达式的用法。
猛然想起两年写了一个java的正规表达式的java工具类,分享一下,有用到的欢迎下载使用。
如果你有常用的定义好的,且测试通过的正规表达式,欢迎跟贴,也让我享用一下 .
类中用到了 jakarta-oro-2.0.jar 包,请大家自己在 apache网站下下载
在这是junit测试单元类我就不提交了,在main()方法中有几个小测试,有兴趣自己玩吧.
这个工具类目前主要有25种正规表达式(有些不常用,但那时才仔细深入的研究了一下正规,写上瘾了,就当时能想到的都写了):
1.匹配图象; 2 匹配email地址; 3 匹配匹配并提取url ; 4 匹配并提取http ;
5.匹配日期 6 匹配电话; 7 匹配身份证 8 匹配邮编代码
9. 不包括特殊字符的匹配 (字符串中不包括符号 数学次方号^ 单引号' 双引号" 分号; 逗号, 帽号: 数学减号- 右尖括号> 左尖括号< 反斜杠\ 即空格,制表符,回车符等
10 匹配非负整数(正整数 + 0) 11 匹配不包括零的非负整数(正整数 > 0)
12 匹配正整数 13 匹配非正整数(负整数 + 0)
14 匹配负整数; 15. 匹配整数 ;
16 匹配非负浮点数(正浮点数 + 0) 17. 匹配正浮点数
18 匹配非正浮点数(负浮点数 + 0) 19 匹配负浮点数;
20 .匹配浮点数; 21. 匹配由26个英文字母组成的字符串;
22. 匹配由26个英文字母的大写组成的字符串 23 匹配由26个英文字母的小写组成的字符串
24 匹配由数字和26个英文字母组成的字符串; 25 匹配由数字、26个英文字母或者下划线组成的字符串;
java 代码
-
- package com.ygj.util;
-
- import java.util.*;
-
- import org.apache.oro.text.regex.*;
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public final class Regexp
- {
-
-
- static final Set SEPARATOR_SET=new TreeSet();
- {
- SEPARATOR_SET.add("(");
- SEPARATOR_SET.add(")");
- SEPARATOR_SET.add("[");
- SEPARATOR_SET.add("]");
- SEPARATOR_SET.add("{");
- SEPARATOR_SET.add("}");
- SEPARATOR_SET.add("<");
- SEPARATOR_SET.add(">");
- }
-
-
-
- public static HashMap regexpHash = new HashMap();
-
-
- public static List matchingResultList = new ArrayList();
-
- private Regexp()
- {
-
- }
-
-
-
-
- public static Regexp getInstance()
- {
- return new Regexp();
- }
-
-
-
-
-
-
-
-
-
-
-
- public static final String icon_regexp = "^(/{0,1}\\w){1,}\\.(gif|dmp|png|jpg)$|^\\w{1,}\\.(gif|dmp|png|jpg)$";
-
-
-
-
-
-
-
-
-
-
-
- public static final String email_regexp = "(?:\\w[-._\\w]*\\w@\\w[-._\\w]*\\w\\.\\w{2,3}$)";
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public static final String url_regexp = "(\\w+)://([^/:]+)(:\\d*)?([^#\\s]*)";
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public static final String http_regexp = "(http|https|ftp)://([^/:]+)(:\\d*)?([^#\\s]*)";
-
-
-
-
-
-
-
-
-
-
-
-
-
- public static final String date_regexp = "^((((19){1}|(20){1})d{2})|d{2})[-\\s]{1}[01]{1}d{1}[-\\s]{1}[0-3]{1}d{1}$";
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public static final String phone_regexp = "^(?:0[0-9]{2,3}[-\\s]{1}|\\(0[0-9]{2,4}\\))[0-9]{6,8}$|^[1-9]{1}[0-9]{5,7}$|^[1-9]{1}[0-9]{10}$";
-
-
-
-
-
-
-
-
-
-
-
-
- public static final String ID_card_regexp = "^\\d{10}|\\d{13}|\\d{15}|\\d{18}$";
-
-
-
-
-
-
-
-
-
-
-
- public static final String ZIP_regexp = "^[0-9]{6}$";
-
-
-
-
-
-
-
-
-
-
-
-
- public static final String non_special_char_regexp = "^[^'\"\\;,:-<>\\s].+$";
-
-
-
-
-
- public static final String non_negative_integers_regexp = "^\\d+$";
-
-
-
-
- public static final String non_zero_negative_integers_regexp = "^[1-9]+\\d*$";
-
-
-
-
-
-
- public static final String positive_integer_regexp = "^[0-9]*[1-9][0-9]*$";
-
-
-
-
-
-
- public static final String non_positive_integers_regexp = "^((-\\d+)|(0+))$";
-
-
-
-
-
-
- public static final String negative_integers_regexp = "^-[0-9]*[1-9][0-9]*$";
-
-
-
-
-
-
- public static final String integer_regexp = "^-?\\d+$";
-
-
-
-
-
-
- public static final String non_negative_rational_numbers_regexp = "^\\d+(\\.\\d+)?$";
-
-
-
-
-
-
- public static final String positive_rational_numbers_regexp = "^(([0-9]+\\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\\.[0-9]+)|([0-9]*[1-9][0-9]*))$";
-
-
-
-
-
-
- public static final String non_positive_rational_numbers_regexp = "^((-\\d+(\\.\\d+)?)|(0+(\\.0+)?))$";
-
-
-
-
-
-
- public static final String negative_rational_numbers_regexp = "^(-(([0-9]+\\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\\.[0-9]+)|([0-9]*[1-9][0-9]*)))$";
-
-
-
-
-
-
- public static final String rational_numbers_regexp = "^(-?\\d+)(\\.\\d+)?$";
-
-
-
-
-
-
- public static final String letter_regexp = "^[A-Za-z]+$";
-
-
-
-
-
-
- public static final String upward_letter_regexp = "^[A-Z]+$";
-
-
-
-
-
-
- public static final String lower_letter_regexp = "^[a-z]+$";
-
-
-
-
-
-
- public static final String letter_number_regexp = "^[A-Za-z0-9]+$";
-
-
-
-
-
-
- public static final String letter_number_underline_regexp = "^\\w+$";
-
-
-
-
-
-
-
-
-
- public void putRegexpHash(String regexpName, String regexp)
- {
- regexpHash.put(regexpName, regexp);
- }
-
-
-
-
-
-
-
-
-
- public String getRegexpHash(String regexpName)
- {
- if (regexpHash.get(regexpName) != null)
- {
- return ((String) regexpHash.get(regexpName));
- }
- else
- {
- System.out.println("在regexpHash中没有此正规表达式");
- return "";
- }
- }
-
-
-
-
- public void clearRegexpHash()
- {
- regexpHash.clear();
- return;
- }
-
-
-
-
-
-
-
-
-
-
-
-
- public static boolean isHardRegexpValidate(String source, String regexp)
- {
-
- try
- {
-
- PatternCompiler compiler = new Perl5Compiler();
-
-
- PatternMatcher matcher = new Perl5Matcher();
-
-
- Pattern hardPattern = compiler.compile(regexp);
-
-
- return matcher.contains(source, hardPattern);
-
- }
- catch (MalformedPatternException e)
- {
- e.printStackTrace();
-
- }
- return false;
- }
-
http://www.javaeye.com/topic/67398