一个java正规表达式工具类

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值