您的位置:首页 > 其它

常用验证功能正则表达式及使用正则表达式进行验证

2012-09-21 13:42 465 查看
1 Java中使用正则表达式

String rexExpStr = "正则表达式串";//正则表达式

Strng targetStr = "待验证字符串";//要想进行验证的字符串内容

Pattern p_postcode = Pattern.compile(rexExpStr);

Matcher m_postcode = p_postcode.matcher(targetStr);

if (m_postcode.find()) {//满足验证条件

//Business codes

........

}else{//不满足验证条件

.......

}

2 常用的正则表达式

public final static String REG_int = "^\\d*$";

public final static String REG_float = "^\\d+(\\.\\d+)?$";

public final static String REG_date = "^[\\d]{4}([-][\\d]{2}){2}$";

public final static String REG_time = "^[\\d]{4}([-][\\d]{2}){2}([ ][12]{1}[\\d]{1}([:][123456]{1}[\\d]{1}){2}){1}$";

public final static String REG_datetime = "^[\\d]{4}([-][\\d]{2}){2}([ ][12]{1}[\\d]{1}([:][123456]{1}[\\d]{1}){2}){1}$";

public final static String REG_email = "^[\\w\\d]+@[\\w\\d]+(+)+$]\\.[\\w\\d]+)+$";

public final static String REG_phone = "^(){0,2}\\d+$]\\d+[-]){0,2}\\d+$";

public final static String REG_mobile = "^[1]\\d{10}$";

public final static String REG_postcode = "^\\d{6}$";

public final static String REG_url = "^[\\w]+[:][/]{2}([\\w]+\\.)+([\\w]+[/])+[\\w\\.]*$";

public final static String REG_year = "^\\d{4}$";

public final static String REG_month = "^\\d{1,2}$";

public final static String REG_day = "^\\d{1,2}$";
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: