一个正则表达式工具类

一个java正规表达式工具类
类中用到了 jakarta-oro-2.0.jar 包,请大家自己在 apache网站下下载

在这是junit测试单元类我就不提交了,在main()方法中有几个小测试,有兴趣自己玩吧.

这个工具类目前主要有25种正规表达式(有些不常用,但那时才仔细深入的研究了一下正规,写上瘾了,就当时能想到的都写了):
匹配图象; 2 匹配email地址; 3 匹配匹配并提取url ; 4 匹配并提取http ;
匹配日期 6 匹配电话; 7 匹配身份证 8 匹配邮编代码
不包括特殊字符的匹配 (字符串中不包括符号 数学次方号^ 单引号' 双引号" 分号; 逗号, 帽号: 数学减号- 右尖括号> 左尖括号< 反斜杠\ 即空格,制表符,回车符等

10 匹配非负整数(正整数 + 0) 11 匹配不包括零的非负整数(正整数 > 0)

12 匹配正整数 13 匹配非正整数(负整数 + 0)

14 匹配负整数; 15. 匹配整数 ;

16 匹配非负浮点数(正浮点数 + 0) 17. 匹配正浮点数

18 匹配非正浮点数(负浮点数 + 0) 19 匹配负浮点数;

20 .匹配浮点数; 21. 匹配由26个英文字母组成的字符串;
匹配由26个英文字母的大写组成的字符串 23 匹配由26个英文字母的小写组成的字符串

24 匹配由数字和26个英文字母组成的字符串; 25 匹配由数字、26个英文字母或者下划线组成的字符串;

   1. java 代码   
   2. import java.util.*;  
   3. import org.apache.oro.text.regex.*;  
   4.   
   5. /** 
   6.  * 类简介: 使用正则表达式验证数据或提取数据,类中的方法全为静态的 主要方法:1. isHardRegexpValidate(String source, 
   7.  * String regexp) 
   8.  *  
   9.  *  
  10.  * 区分大小写敏感的正规表达式批配 2. isSoftRegexpValidate(String source, String regexp) 
  11.  * 不区分大小写的正规表达式批配 3. getHardRegexpMatchResult(String source, String regexp) 
  12.  * 返回许要的批配结果集(大小写敏感的正规表达式批配) 4. getSoftRegexpMatchResult(String source, String 
  13.  * regexp) 返回许要的批配结果集(不区分大小写的正规表达式批配) 5 getHardRegexpArray(String source, String 
  14.  * regexp) 返回许要的批配结果集(大小写敏感的正规表达式批配) 6. getSoftRegexpMatchResult(String source, 
  15.  * String regexp) 返回许要的批配结果集(不区分大小写的正规表达式批配) 7. getBetweenSeparatorStr(final 
  16.  * String originStr,final char leftSeparator,final char rightSeparator) 
  17.  * 得到指定分隔符中间的字符串的集合 
  18.  *  
  19.  * @mail <A href="mailto:wuzhi2000@hotmail.com">wuzhi2000@hotmail.com</A> 
  20.  * @author ygj 
  21.  *  
  22.  */  
  23. public final class Regexp {  
  24.   /** 保放有四组对应分隔符 */  
  25.   static final Set SEPARATOR_SET = new TreeSet();  
  26.   {  
  27.     SEPARATOR_SET.add("(");  
  28.     SEPARATOR_SET.add(")");  
  29.     SEPARATOR_SET.add("[");  
  30.     SEPARATOR_SET.add("]");  
  31.     SEPARATOR_SET.add("{");  
  32.     SEPARATOR_SET.add("}");  
  33.     SEPARATOR_SET.add("<");  
  34.     SEPARATOR_SET.add(">");  
  35.   }  
  36.   /** 存放各种正规表达式(以key->value的形式) */  
  37.   public static HashMap regexpHash = new HashMap();  
  38.   /** 存放各种正规表达式(以key->value的形式) */  
  39.   public static List matchingResultList = new ArrayList();  
  40.   
  41.   private Regexp() {  
  42.   }  
  43.   
  44.   /** 
  45.    * 返回 Regexp 实例 
  46.    *  
  47.    * @return 
  48.    */  
  49.   public static Regexp getInstance() {  
  50.     return new Regexp();  
  51.   }  
  52.   
  53.   /** 
  54.    * 匹配图象 
  55.    *  
  56.    * 格式: /相对路径/文件名.后缀 (后缀为gif,dmp,png) 
  57.    *  
  58.    * 匹配 : /forum/head_icon/admini2005111_ff.gif 或 admini2005111.dmp 
  59.    *  
  60.    * 不匹配: c:/admins4512.gif 
  61.    *  
  62.    */  
  63.   public static final String icon_regexp = "^(/{0,1}\\w){1,}\\.(gif|dmp|png|jpg)$|^\\w{1,}\\.(gif|dmp|png|jpg)$";  
  64.   /** 
  65.    * 匹配email地址 
  66.    *  
  67.    * 格式: <A href="mailto:XXX@XXX.XXX.XX">XXX@XXX.XXX.XX</A> 
  68.    *  
  69.    * 匹配 : <A href="mailto:foo@bar.com">foo@bar.com</A> 或 < A href="mailto:foobar@foobar.com.au">foobar@foobar.com.au</A> 
  70.    *  
  71.    * 不匹配: foo@bar 或 $$$@bar.com 
  72.    *  
  73.    */  
  74.   public static final String email_regexp = "(?:\\w[-._\\w]*\\w@\\w[-._\\w]*\\w\\.\\w{2,3}$)";  
  75.   /** 
  76.    * 匹配匹配并提取url 
  77.    *  
  78.    * 格式: XXXX://XXX.XXX.XXX.XX/XXX.XXX?XXX=XXX 
  79.    *  
  80.    * 匹配 : <A href="http://www.suncer.com" target=_blank>http://www.suncer.com</A> 或news://www 
  81.    *  
  82.    * 提取(MatchResult matchResult=matcher.getMatch()): matchResult.group(0)= 
  83.    * <A href="http://www.suncer.com:8080/index.html?login=true" target=_blank>http://www.suncer.com:8080/index.html?login=true</A> matchResult.group(1) = 
  84.    * http matchResult.group(2) = <A href="www.suncer.com" target=_blank>www.suncer.com</A> matchResult.group(3) = :8080 
  85.    * matchResult.group(4) = /index.html?login=true 
  86.    *  
  87.    * 不匹配: c:\window 
  88.    *  
  89.    */  
  90.   public static final String url_regexp = "(\\w+)://([^/:]+)(:\\d*)?([^#\\s]*)";  
  91.   /** 
  92.    * 匹配并提取http 
  93.    *  
  94.    * 格式: <A href="http://XXX.XXX.XXX.XX/XXX.XXX?XXX=XXX" target=_blank> http://XXX.XXX.XXX.XX/XXX.XXX?XXX=XXX</A> 或 <A href="ftp: //XXX.XXX.XXX" target=_blank>ftp://XXX.XXX.XXX</A> 或 https: //XXX 
  95.    *  
  96.    * 匹配 : <A href="http://www.suncer.com:8080/index.html?login=true" target=_blank>http://www.suncer.com:8080/index.html?login=true</A> 
  97.    *  
  98.    * 提取(MatchResult matchResult=matcher.getMatch()): matchResult.group(0)= 
  99.    * <A href="http://www.suncer.com:8080/index.html?login=true" target=_blank>http://www.suncer.com:8080/index.html?login=true</A> matchResult.group(1) = 
 100.    * http matchResult.group(2) = <A href="www.suncer.com" target=_blank>www.suncer.com</A> matchResult.group(3) = :8080 
 101.    * matchResult.group(4) = /index.html?login=true 
 102.    *  
 103.    * 不匹配: news://www 
 104.    *  
 105.    */  
 106.   public static final String http_regexp = "(http|https|ftp)://([^/:]+)(:\\d*)?([^#\\s]*)";  
 107.   /** 
 108.    * 匹配日期 
 109.    *  
 110.    * 格式(首位不为0): XXXX-XX-XX 或 XXXX XX XX 或 XXXX-X-X 
 111.    *  
 112.    * 范围:1900--2099 
 113.    *  
 114.    * 匹配 : 2005-04-04 
 115.    *  
 116.    * 不匹配: 01-01-01 
 117.    *  
 118.    */  
 119.   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}$";// 匹配日期  
 120.   /** 
 121.    * 匹配电话 
 122.    *  
 123.    * 格式为: 0XXX-XXXXXX(10-13位首位必须为0) 或0XXX XXXXXXX(10-13位首位必须为0) 或 
 124.    * (0XXX)XXXXXXXX(11-14位首位必须为0) 或 XXXXXXXX(6-8位首位不为0) 或 XXXXXXXXXXX(11位首位不为0) 
 125.    *  
 126.    * 匹配 : 0371-123456 或 (0371)1234567 或 (0371)12345678 或 010-123456 或 
 127.    * 010-12345678 或 12345678912 
 128.    *  
 129.    * 不匹配: 1111-134355 或 0123456789 
 130.    *  
 131.    */  
 132.   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}$";  
 133.   /** 
 134.    * 匹配身份证 
 135.    *  
 136.    * 格式为: XXXXXXXXXX(10位) 或 XXXXXXXXXXXXX(13位) 或 XXXXXXXXXXXXXXX(15位) 或 
 137.    * XXXXXXXXXXXXXXXXXX(18位) 
 138.    *  
 139.    * 匹配 : 0123456789123 
 140.    *  
 141.    * 不匹配: 0123456 
 142.    *  
 143.    */  
 144.   public static final String ID_card_regexp = "^\\d{10}|\\d{13}|\\d{15}|\\d{18}$";  
 145.   /** 
 146.    * 匹配邮编代码 
 147.    *  
 148.    * 格式为: XXXXXX(6位) 
 149.    *  
 150.    * 匹配 : 012345 
 151.    *  
 152.    * 不匹配: 0123456 
 153.    *  
 154.    */  
 155.   public static final String ZIP_regexp = "^[0-9]{6}$";// 匹配邮编代码  
 156.   /** 
 157.    * 不包括特殊字符的匹配 (字符串中不包括符号 数学次方号^ 单引号' 双引号" 分号; 逗号, 帽号: 数学减号- 右尖括号> 左尖括号< 反斜杠\ 
 158.    * 即空格,制表符,回车符等 ) 
 159.    *  
 160.    * 格式为: x 或 一个一上的字符 
 161.    *  
 162.    * 匹配 : 012345 
 163.    *  
 164.    * 不匹配: 0123456 
 165.    *  
 166.    */  
 167.   public static final String non_special_char_regexp = "^[^'\"\\;,:-<>\\s].+$";// 匹配邮编代码  
 168.   /** 
 169.    * 匹配非负整数(正整数 + 0) 
 170.    */  
 171.   public static final String non_negative_integers_regexp = "^\\d+$";  
 172.   /** 
 173.    * 匹配不包括零的非负整数(正整数 > 0) 
 174.    */  
 175.   public static final String non_zero_negative_integers_regexp = "^[1-9]+\\d*$";  
 176.   /** 
 177.    *  
 178.    * 匹配正整数 
 179.    *  
 180.    */  
 181.   public static final String positive_integer_regexp = "^[0-9]*[1-9][0-9]*$";  
 182.   /** 
 183.    *  
 184.    * 匹配非正整数(负整数 + 0) 
 185.    *  
 186.    */  
 187.   public static final String non_positive_integers_regexp = "^((-\\d+)|(0+))$";  
 188.   /** 
 189.    *  
 190.    * 匹配负整数 
 191.    *  
 192.    */  
 193.   public static final String negative_integers_regexp = "^-[0-9]*[1-9][0-9]*$";  
 194.   /** 
 195.    *  
 196.    * 匹配整数 
 197.    *  
 198.    */  
 199.   public static final String integer_regexp = "^-?\\d+$";  
 200.   /** 
 201.    *  
 202.    * 匹配非负浮点数(正浮点数 + 0) 
 203.    *  
 204.    */  
 205.   public static final String non_negative_rational_numbers_regexp = "^\\d+(\\.\\d+)?$";  
 206.   /** 
 207.    *  
 208.    * 匹配正浮点数 
 209.    *  
 210.    */  
 211.   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]*))$";  
 212.   /** 
 213.    *  
 214.    * 匹配非正浮点数(负浮点数 + 0) 
 215.    *  
 216.    */  
 217.   public static final String non_positive_rational_numbers_regexp = "^((-\\d+(\\.\\d+)?)|(0+(\\.0+)?))$";  
 218.   /** 
 219.    *  
 220.    * 匹配负浮点数 
 221.    *  
 222.    */  
 223.   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]*)))$";  
 224.   /** 
 225.    *  
 226.    * 匹配浮点数 
 227.    *  
 228.    */  
 229.   public static final String rational_numbers_regexp = "^(-?\\d+)(\\.\\d+)?$";  
 230.   /** 
 231.    *  
 232.    * 匹配由26个英文字母组成的字符串 
 233.    *  
 234.    */  
 235.   public static final String letter_regexp = "^[A-Za-z]+$";  
 236.   /** 
 237.    *  
 238.    * 匹配由26个英文字母的大写组成的字符串 
 239.    *  
 240.    */  
 241.   public static final String upward_letter_regexp = "^[A-Z]+$";  
 242.   /** 
 243.    *  
 244.    * 匹配由26个英文字母的小写组成的字符串 
 245.    *  
 246.    */  
 247.   public static final String lower_letter_regexp = "^[a-z]+$";  
 248.   /** 
 249.    *  
 250.    * 匹配由数字和26个英文字母组成的字符串 
 251.    *  
 252.    */  
 253.   public static final String letter_number_regexp = "^[A-Za-z0-9]+$";  
 254.   /** 
 255.    *  
 256.    * 匹配由数字、26个英文字母或者下划线组成的字符串 
 257.    *  
 258.    */  
 259.   public static final String letter_number_underline_regexp = "^\\w+$";  
 260.   
 261.   /** 
 262.    * 添加正规表达式 (以key->value的形式存储) 
 263.    *  
 264.    * @param regexpName 
 265.    *          该正规表达式名称 ` 
 266.    * @param regexp 
 267.    *          该正规表达式内容 
 268.    */  
 269.   public void putRegexpHash(String regexpName, String regexp) {  
 270.     regexpHash.put(regexpName, regexp);  
 271.   }  
 272.   
 273.   /** 
 274.    * 得到正规表达式内容 (通过key名提取出value[正规表达式内容]) 
 275.    *  
 276.    * @param regexpName 
 277.    *          正规表达式名称 
 278.    *  
 279.    * @return 正规表达式内容 
 280.    */  
 281.   public String getRegexpHash(String regexpName) {  
 282.     if (regexpHash.get(regexpName) != null) {  
 283.       return ((String) regexpHash.get(regexpName));  
 284.     } else {  
 285.       System.out.println("在regexpHash中没有此正规表达式");  
 286.       return "";  
 287.     }  
 288.   }  
 289.   
 290.   /** 
 291.    * 清除正规表达式存放单元 
 292.    */  
 293.   public void clearRegexpHash() {  
 294.     regexpHash.clear();  
 295.     return;  
 296.   }  
 297.   
 298.   /** 
 299.    * 大小写敏感的正规表达式批配 
 300.    *  
 301.    * @param source 
 302.    *          批配的源字符串 
 303.    *  
 304.    * @param regexp 
 305.    *          批配的正规表达式 
 306.    *  
 307.    * @return 如果源字符串符合要求返回真,否则返回假 如: 
 308.    *         Regexp.isHardRegexpValidate("ygj@suncer.com.cn",email_regexp) 返回真 
 309.    */  
 310.   public static boolean isHardRegexpValidate(String source, String regexp) {  
 311.     try {  
 312.       // 用于定义正规表达式对象模板类型  
 313.       PatternCompiler compiler = new Perl5Compiler();  
 314.       // 正规表达式比较批配对象  
 315.       PatternMatcher matcher = new Perl5Matcher();  
 316.       // 实例大小大小写敏感的正规表达式模板  
 317.       Pattern hardPattern = compiler.compile(regexp);  
 318.       // 返回批配结果  
 319.       return matcher.contains(source, hardPattern);  
 320.     } catch (MalformedPatternException e) {  
 321.       e.printStackTrace();  
 322.     }  
 323.     return false;  
 324.   }  
 325. }  
 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值