Java正則表達式的使用

import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegexBean {
Pattern p = null;
Matcher m = null;
String str1 = "^[0-9]{1,20}$"; //校驗是否全由數字組成
String str2 = "^[a-zA-Z]{1}([a-zA-Z0-9]|[._]){4,19}$";
//校驗登錄名:只能輸入5-20個以字母開頭、可帶數字、“_”、“.”的字串
String str3 = "^(\\w){6,20}$"; //校驗密碼:只能輸入6-20個字母、數字、下劃線
String str4 = "^[+]{0,1}(\\d){1,3}[ ]?([-]?((\\d)|[ ]){1,12})+$";
//校驗普通電話、傳真號碼:可以“+”開頭,除數字外,可含有“-”
String str5 = "^[+]{0,1}(\\d){1,3}[ ]?([-]?((\\d)|[ ]){1,12})+$"; //校驗手機號碼
String str6 = "^[a-zA-Z0-9 ]{3,6}$"; //校驗郵政編碼
String str7 = "^[0-9.]{1,20}$"; //ip
String str8 = "^\\d+$"; //非負整數
String str9 = "^[0-9]*[1-9][0-9]*$"; //正整數
String str10 = "^[A-Za-z]+$"; //由26個英文字母組成的字符串
String str11 = "^\\w+$"; //由數字、26個英文字母或者下劃線組成的字符串
String str12 = "^[\\w-]+(\\.[\\w-]+)*@[\\w-]+(\\.[\\w-]+)+$"; //email
String str13 = "^[a-zA-z]+://(\\w+(-\\w+)*)(\\.(\\w+(-\\w+)*))*(\\?\\S*)?$";
//url
String str14 = "[0-9]{4}\\-[0-9]{1,2}\\-[0-9]{1,2}";
String str15 =
"^((((19|20)(([02468][048])|([13579][26]))\\-02\\-29))|((20[0-9][0-9])|(19[0-9][0-9]))\\-((((0[1-9])|(1[0-2]))\\-((0[1-9])|(1\\d)|(2[0-8])))|((((0[13578])|(1[02]))\\-31)|(((01,3-9])|(1[0-2]))\\-(29|30)))))$";
//date 如2004-06-06,非常精確,如2004-06-32可以查出非法,高興吧!!
String str16 =
"^(\\d{4})\\-(\\d{2})\\-(\\d{2}) (\\d{2}):(\\d{2}):(\\d{2})$";
//date and time 如2004-11-12 12:10:16
String str17 = "^([\u4e00-\u9fa5]*)";//漢字
public RegexBean() {}
public boolean isDate(String str) {
p = Pattern.compile(str15);
m = p.matcher(str);
boolean b = m.matches();
if (b)
   return true;
else
   return false;
}
public boolean isEmail(String str) {
p = Pattern.compile(str12);
m = p.matcher(str);
boolean b = m.matches();
if (b)
   return true;
else
   return false;
}
public boolean isDateAndTime(String str) {
p = Pattern.compile(str16);
m = p.matcher(str);
boolean b = m.matches();
if (b)
   return true;
else
   return false;
}
public boolean isUserName(String str) {
p = Pattern.compile(str2);
m = p.matcher(str);
boolean b = m.matches();
if (b)
   return true;
else
   return false;
}
public boolean isPassword(String str) {
p = Pattern.compile(str3);
m = p.matcher(str);
boolean b = m.matches();
if (b)
   return true;
else
   return false;
}
public boolean isPhone(String str) {
p = Pattern.compile(str4);
m = p.matcher(str);
boolean b = m.matches();
if (b)
   return true;
else
   return false;
}
public boolean isMobile(String str) {
p = Pattern.compile(str5);
m = p.matcher(str);
boolean b = m.matches();
if (b)
   return true;
else
   return false;
}
public boolean isPost(String str) {
p = Pattern.compile(str6);
m = p.matcher(str);
boolean b = m.matches();
if (b)
   return true;
else
   return false;
}
public boolean isIP(String str) {
p = Pattern.compile(str7);
m = p.matcher(str);
boolean b = m.matches();
if (b)
   return true;
else
   return false;
}
public boolean isChinese(String str) {
p = Pattern.compile(str17);
m = p.matcher(str);
boolean b = m.matches();
if (b)
   return true;
else
   return false;
}
public boolean isURL(String str) {
p = Pattern.compile(str13);
m = p.matcher(str);
boolean b = m.matches();
if (b)
   return true;
else
   return false;
}
public boolean isDigit(String str) {
p = Pattern.compile(str1);
m = p.matcher(str);
boolean b = m.matches();
if (b)
   return true;
else
   return false;
}
public static void main(String[] args) {
RegexBean bb = new RegexBean();
String str = "仗劍動";
System.out.println(bb.isChinese(str));
}
}

 

 

發佈了84 篇原創文章 · 獲贊 1 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章