java的正规表达式的java工具类-asp.net关注

类中用到了 jakarta-oro-2.0.jar 包,请大家自己在 apache网站下下载

在这是junit测试单元类我就不提交了,在main()方法中有几个小测试

这个工具类目前主要有25种正规表达式(有些不常用,但那时才仔细深入的研究了一下正规):

1.匹配图象; 2 匹配email地址;3 匹配匹配并提取url;4 匹配并提取?login=true

  • *matchResult.group(1)=?login=true
  • *
  • *不匹配:c:\window
  • *
  • */
  • publicstaticfinalStringurl_regexp="(\\w+)://([^/:]+)(:\\d*)?([^#\\s]*)";
  • /**
  • *匹配并提取?login=true
  • *
  • *提取(MatchResultmatchResult=matcher.getMatch()):
  • *matchResult.group(0)=:8080/index.html?login=true
  • *matchResult.group(1)=?login=true
  • *
  • *不匹配:news://www
  • *
  • */
  • publicstaticfinalStringhttp_regexp="(http|https|ftp)://([^/:]+)(:\\d*)?([^#\\s]*)";
  • /**
  • *匹配日期
  • *
  • *格式(首位不为0):XXXX-XX-XX或XXXXXXXX或XXXX-X-X
  • *
  • *范围:1900--2099
  • *
  • *匹配:2005-04-04
  • *
  • *不匹配:01-01-01
  • *
  • */
  • publicstaticfinalStringdate_regexp="^((((19){1}|(20){1})d{2})|d{2})[-\\s]{1}[01]{1}d{1}[-\\s]{1}[0-3]{1}d{1}$";//匹配日期
  • /**
  • *匹配电话
  • *
  • *格式为:0XXX-XXXXXX(10-13位首位必须为0)或0XXXXXXXXXX(10-13位首位必须为0)或
  • *(0XXX)XXXXXXXX(11-14位首位必须为0)或XXXXXXXX(6-8位首位不为0)或
  • *XXXXXXXXXXX(11位首位不为0)
  • *
  • *匹配:0371-123456或(0371)1234567或(0371)12345678或010-123456或
  • *010-12345678或12345678912
  • *
  • *不匹配:1111-134355或0123456789
  • *
  • */
  • publicstaticfinalStringphone_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}$";
  • /**
  • *匹配身份证
  • *
  • *格式为:XXXXXXXXXX(10位)或XXXXXXXXXXXXX(13位)或XXXXXXXXXXXXXXX(15位)或
  • *XXXXXXXXXXXXXXXXXX(18位)
  • *
  • *匹配:0123456789123
  • *
  • *不匹配:0123456
  • *
  • */
  • publicstaticfinalStringID_card_regexp="^\\d{10}|\\d{13}|\\d{15}|\\d{18}$";
  • /**
  • *匹配邮编代码
  • *
  • *格式为:XXXXXX(6位)
  • *
  • *匹配:012345
  • *
  • *不匹配:0123456
  • *
  • */
  • publicstaticfinalStringZIP_regexp="^[0-9]{6}$";//匹配邮编代码
  • /**
  • *不包括特殊字符的匹配(字符串中不包括符号数学次方号^单引号'双引号"分号;逗号,帽号:数学减号-右尖括号>左尖括号<反斜杠\即空格,制表符,回车符等)
  • *
  • *格式为:x或一个一上的字符
  • *
  • *匹配:012345
  • *
  • *不匹配:0123456
  • *
  • */
  • publicstaticfinalStringnon_special_char_regexp="^[^'\"\\;,:-<>\\s].+$";//匹配邮编代码
  • /**
  • *匹配非负整数(正整数+0)
  • */
  • publicstaticfinalStringnon_negative_integers_regexp="^\\d+$";
  • /**
  • *匹配不包括零的非负整数(正整数>0)
  • */
  • publicstaticfinalStringnon_zero_negative_integers_regexp="^[1-9]+\\d*$";
  • /**
  • *
  • *匹配正整数
  • *
  • */
  • publicstaticfinalStringpositive_integer_regexp="^[0-9]*[1-9][0-9]*$";
  • /**
  • *
  • *匹配非正整数(负整数+0)
  • *
  • */
  • publicstaticfinalStringnon_positive_integers_regexp="^((-\\d+)|(0+))$";
  • /**
  • *
  • *匹配负整数
  • *
  • */
  • publicstaticfinalStringnegative_integers_regexp="^-[0-9]*[1-9][0-9]*$";
  • /**
  • *
  • *匹配整数
  • *
  • */
  • publicstaticfinalStringinteger_regexp="^-?\\d+$";
  • /**
  • *
  • *匹配非负浮点数(正浮点数+0)
  • *
  • */
  • publicstaticfinalStringnon_negative_rational_numbers_regexp="^\\d+(\\.\\d+)?$";
  • /**
  • *
  • *匹配正浮点数
  • *
  • */
  • publicstaticfinalStringpositive_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]*))$";
  • /**
  • *
  • *匹配非正浮点数(负浮点数+0)
  • *
  • */
  • publicstaticfinalStringnon_positive_rational_numbers_regexp="^((-\\d+(\\.\\d+)?)|(0+(\\.0+)?))$";
  • /**
  • *
  • *匹配负浮点数
  • *
  • */
  • publicstaticfinalStringnegative_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]*)))$";
  • /**
  • *
  • *匹配浮点数
  • *
  • */
  • publicstaticfinalStringrational_numbers_regexp="^(-?\\d+)(\\.\\d+)?$";
  • /**
  • *
  • *匹配由26个英文字母组成的字符串
  • *
  • */
  • publicstaticfinalStringletter_regexp="^[A-Za-z]+$";
  • /**
  • *
  • *匹配由26个英文字母的大写组成的字符串
  • *
  • */
  • publicstaticfinalStringupward_letter_regexp="^[A-Z]+$";
  • /**
  • *
  • *匹配由26个英文字母的小写组成的字符串
  • *
  • */
  • publicstaticfinalStringlower_letter_regexp="^[a-z]+$";
  • /**
  • *
  • *匹配由数字和26个英文字母组成的字符串
  • *
  • */
  • publicstaticfinalStringletter_number_regexp="^[A-Za-z0-9]+$";
  • /**
  • *
  • *匹配由数字、26个英文字母或者下划线组成的字符串
  • *
  • */
  • publicstaticfinalStringletter_number_underline_regexp="^\\w+$";
  • /**
  • *添加正规表达式(以key->value的形式存储)
  • *
  • *@paramregexpName
  • *该正规表达式名称`
  • *@paramregexp
  • *该正规表达式内容
  • */
  • publicvoidputRegexpHash(StringregexpName,Stringregexp)
  • {
  • regexpHash.put(regexpName,regexp);
  • }
  • /**
  • *得到正规表达式内容(通过key名提取出value[正规表达式内容])
  • *
  • *@paramregexpName
  • *正规表达式名称
  • *
  • *@return正规表达式内容
  • */
  • publicStringgetRegexpHash(StringregexpName)
  • {
  • if(regexpHash.get(regexpName)!=null)
  • {
  • return((String)regexpHash.get(regexpName));
  • }
  • else
  • {
  • System.out.println("在regexpHash中没有此正规表达式");
  • return"";
  • }
  • }
  • /**
  • *清除正规表达式存放单元
  • */
  • publicvoidclearRegexpHash()
  • {
  • regexpHash.clear();
  • return;
  • }
  • /**
  • *大小写敏感的正规表达式批配
  • *
  • *@paramsource
  • *批配的源字符串
  • *
  • *@paramregexp
  • *批配的正规表达式
  • *
  • *@return如果源字符串符合要求返回真,否则返回假如:Regexp.isHardRegexpValidate("ygj@suncer.com.cn",email_regexp)返回真
  • */
  • publicstaticbooleanisHardRegexpValidate(Stringsource,Stringregexp)
  • {
  • try
  • {
  • //用于定义正规表达式对象模板类型
  • PatternCompilercompiler=newPerl5Compiler();
  • //正规表达式比较批配对象
  • PatternMatchermatcher=newPerl5Matcher();
  • //实例大小大小写敏感的正规表达式模板
  • PatternhardPattern=compiler.compile(regexp);
  • //返回批配结果
  • returnmatcher.contains(source,hardPattern);
  • }
  • catch(MalformedPatternExceptione)
  • {
  • e.printStackTrace();
  • }
  • returnfalse;
  • }

  • posted @ 2011-01-22 20:57  程承JAVA  阅读(212)  评论(0编辑  收藏  举报