今天加班,一個同事讓我給他講解一下正規表達式的用法。
猛然想起兩年寫了一個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