zoukankan      html  css  js  c++  java
  • Java正则表达式工具类实例

    Java正则表达式工具类实例

    2009-06-10 13:36 dolphin_ygj JavaEye 我要评论(0) 字号:T | T
    一键收藏,随时查看,分享好友!

    本文向您展示一个Java正则表达式的工具类,这个工具类包含25中正则表达式,本文列出15中常用功能。

    AD: 2013大数据全球技术峰会低价抢票中

    以前写了一个Java的正规表达式的Java工具类,分享一下,有用到的欢迎下载使用。如果你有常用的定义好的,且测试通过的正规表达式,欢迎跟贴,也让我享用一下类中用到了 jakarta-oro-2.0.jar包,请大家自己在 apache网站下下载

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

    这个Java正则表达式工具类目前主要有25种正规表达式,有些不常用,这里只列出15种常用Java正则表达式功能。
    1.匹配图象;                      
    2 匹配email地址;                    
    3 匹配匹配并提取url ;                         
    4 匹配并提取http ;
    5.匹配日期                       
    6 匹配电话;                               
    7 匹配身份证                                       
    8 匹配邮编代码
    9. 不包括特殊字符的匹配
    10 匹配非负整数(正整数 + 0)                                         
    11 匹配不包括零的非负整数(正整数 > 0)
    12 匹配正整数                                                                      
    13  匹配非正整数(负整数 + 0)                                                
    14 匹配负整数;                                                                      
    15.匹配整数

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

    上面的工具类代码包含了常用Java正则表达式功能,稍加调试就可以满足实际项目中的需求。

  • 相关阅读:
    leetcode 18 4Sum
    leetcode 71 Simplify Path
    leetcode 10 Regular Expression Matching
    leetcode 30 Substring with Concatenation of All Words
    leetcode 355 Design Twitte
    leetcode LRU Cache
    leetcode 3Sum
    leetcode Letter Combinations of a Phone Number
    leetcode Remove Nth Node From End of List
    leetcode Valid Parentheses
  • 原文地址:https://www.cnblogs.com/adolfmc/p/2918384.html
Copyright © 2011-2022 走看看