Java正则表达式验证
package com;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* java正则表达式验证
*/
public class RegExpTest
{
/**
* 验证QQ号码
*
* @author lihaiming
* @param QQ
* @return
*/
public static boolean checkQQ(String QQ) {
String regex = "^[1-9][0-9]{4,}$";
return check(QQ, regex);
}
private static boolean check(String str, String regex) {
boolean flag = false;
try {
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(str);
flag = matcher.matches();
} catch (Exception e) {
flag = false;
}
return flag;
}//此方法来源于:http://www.jb51.net/article/49409.htm
//java检测是否为电话号码(手机、固定电话验证)
String legalPhone = "";
String regExp ="^((13[0-9])|(15[^4,\\D])|(18[0,5-9]))\\d{8}|[0]{1}[0-9]{2,3}-[0-9]{7,8}$";
Pattern p = Pattern.compile(regExp);
Matcher m = p.matcher(importPotentialBFOs[i].getLegalPhone());
if(m.find()){ //注意:m.find只能用一次,第二次调用后都为false
legalPhone = importPotentialBFOs[i].getLegalPhone();
uploadTmp.setLegalTelephone(legalPhone);
}else{
throw new BizException("联系电话格式错误!");
}//此方法来源于:http://blog.csdn.net/civilized/article/details/35231627
/**
* 日期校验
*
* @author lihaiming
* @param hhmmStr
* 日期字符串(HH:mm)
* @param format
* 日期格式
* @return
*/
public static boolean isHhmm(String hhmmStr) {
Pattern p = Pattern.compile("^(0\\d{1}|1\\d{1}|2[0-3]):[0-5]\\d{1}$");
Matcher m = p.matcher(hhmmStr);
boolean flag = m.matches();
if (flag) {
return true;
} else {
return false;
}
}//参考自:http://zhidao.baidu.com/link?url=PKeLU3kG5QSjXc8ZJ-fAWEhTzy379VQLAyYBWDf2G-fNWeoBuwdoXaMZkO8c3luDy1z-PTI_tXrBl4jVrVDOA5kq2Ys9_UtpMa1fKg4YXkq
/**
* 浮点数校验
*
* @param str
* 字符串
* @return
*/
public static boolean isFloatDigit(String str) {
return str.matches("[\\d]+\\.[\\d]+");
}
/**
* 正则验证方法一
*/
public static boolean regExpTest() {
//验证标识符必须由字母、数字、下划线组成
Pattern p = Pattern.compile("[a-z0-9A-Z_]+");
Matcher m = p.matcher("aasdad");
boolean flg = m.matches();
System.out.println(flg);
return flg;
}
/**
* 正则验证方法二
*/
public static boolean regExpTest2(String str) {
//匹配标识符必须由字母、数字、下划线组成,且开头和结尾不能有下划线
//String regex = "(^[a-z0-9A-Z])[a-z0-9A-Z_]+([a-z0-9-A-Z])";
//匹配标识符必须由字母、数字、下划线组成,且开头和结尾不能有下划线,且中间的字符至少1个不能超过5个
String regex = "(^[a-z0-9A-Z])[a-z0-9A-Z_]{1,5}([a-z0-9-A-Z])";
boolean flg = Pattern.matches(regex, str);
System.out.println(flg);
return flg;
}
/**
* 去掉前后空格
*/
public static String regExpTest3(String str) {
//去掉前后空格方法一
//String regex = "^\\[(.*)\\]$";
//String s1 = str.replaceAll(regex, "$1");
//方法二,注:replace方法无正则匹配
String regex = "^\\[|\\]$";
String s1 = str.replaceAll(regex, "");
System.out.println(s1);
return s1;
}
/**
* 验证邮箱格式"^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$"//email地址
*/
public static boolean regExpTest4(String str) {
String regex = "^[\\w-]+(\\.[\\w-]+)*\\@([\\.\\w-]+)+$";
boolean flg = Pattern.matches(regex, str);
System.out.println(flg);
return flg;
}
/**
* 验证电话号码
*/
public static boolean regExpTest5(String str){
// "(d+-)?(d{4}-?d{7}|d{3}-?d{8}|^d{7,8})(-d+)?" //电话号码
//String regex = "\\d{3}-\\d{4}-\\d{7,8}-\\d{3}";
String regex = "(\\d+-)?(\\d{4}-\\d{7,8})(-\\d+)";
boolean flg = Pattern.matches(regex, str);
System.out.println(flg);
return flg;
}
- /**
- * 手机号验证
- *
- * @param str
- * @return 验证通过返回true
- */
- public static boolean isMobile(String str) {
- Pattern p = null;
- Matcher m = null;
- boolean b = false;
- p = Pattern.compile("^[1][3,4,5,8][0-9]{9}$"); // 验证手机号
- m = p.matcher(str);
- b = m.matches();
- return b;
- }
public static void regExpTest6(String str){
String regex = "(\\d+-)?(\\d{4}-\\d{7,8})(-\\d+)";
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(str);
int count = m.groupCount();
System.out.println(count);
m.find();//尝试查找与该模式匹配的输入序列的下一个子序列。
for(int i=1;i<=count;i++) {
System.out.println(m.group(i));
}
}
/**
* 包含数字或特殊符号
*
* @author lihaiming
* @param str
* 字符串
* @return
*/
public static boolean hasNumberOrSpecialSym(String str) {
String E1 = "[`~!@#$%^&*()+=|{}':;',\\[\\].<>/?~!@#¥%……&*()——+|{}【】‘;:”“’。,、?]";// 英文
String E2 = "[0-9]";// 数字
String temp = null;
for (int i = 0; i < str.length(); i++) {
temp = String.valueOf(str.charAt(i));
if (temp.matches(E1) || temp.matches(E2)) {
return true;
}
}
return false;
}//此方法参考自:http://blog.csdn.net/qiaqia609/article/details/11088101,http://www.sharejs.com/codes/java/6228
public static void main(String[] args)
{
//从键盘读取文件
/* Scanner sn = new Scanner(System.in);
RegExpTest.regExpTest();
RegExpTest.regExpTest2("asdasaa");
RegExpTest.regExpTest3("[aa,bb,cc]");
RegExpTest.regExpTest4("-lgt.q983@hotmail.com.net");
//RegExpTest.regExpTest5(sn.nextLine());
*/
RegExpTest.regExpTest6("029-0913-3237259-020");
}
}