java正则表达式

package demo.java.regex;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
* User: liuwentao
* Date: 2011-4-15 11:18:14
* <p/>
* 说明:
*/
public class RegexUtils {

public static void main(String[] args) {
RegexUtils.checkEmail("liuwentao@gmail.com");
}

/**
* 验证 email是否有效
* @param mail
* @return
*/
public static boolean checkEmail(String mail) {
String regex = "\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*";
return regexMatch(regex,mail);
}

/**
* 通用是否匹配方法
* @param regex
* @param input
* @return
*/
public static boolean regexMatch(String regex,CharSequence input){
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(input);
boolean b = m.find();
System.out.println("b=" + b + ", input=" + input + ", regex=" + regex);
return b;
}
}



类似的还有:

^[a-z]+$  //匹配由26个英文字母的小写组成的字符串
^[A-Z]+$  //匹配由26个英文字母的大写组成的字符串
^[A-Za-z]+$  //匹配由26个英文字母组成的字符串
^[A-Za-z0-9]+$  //匹配由数字和26个英文字母组成的字符串
^\w+$  //匹配由数字、26个英文字母或者下划线组成的字符串
[\u4e00-\u9fa5] //匹配中文字符的正则表达式:
(.*)(\.jpg|\.bmp)$ //只能是jpg和bmp格式
^\d{4}\-\d{1,2}-\d{1,2}$ //只能是2004-10-22格式
^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$  //匹配email地址
^[a-zA-z]+://匹配(\w+(-\w+)*)(\.(\w+(-\w+)*))*(\?\S*)?$  //匹配url


正则表达式--验证手机号码:13[0-9]{9}
实现手机号前带86或是+86的情况:^((\+86)|(86))?(13)\d{9}$
电话号码与手机号码同时验证:(^(\d{3,4}-)?\d{7,8})$|(13[0-9]{9})
提取信息中的网络链接:(h|H)(r|R)(e|E)(f|F) *= *('|")?(\w|\\|\/|\.)+('|"| *|>)?
提取信息中的邮件地址:\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*
提取信息中的图片链接:(s|S)(r|R)(c|C) *= *('|")?(\w|\\|\/|\.)+('|"| *|>)?
提取信息中的IP地址:(\d+)\.(\d+)\.(\d+)\.(\d+)
提取信息中的中国手机号码:(86)*0*13\d{9}
提取信息中的中国固定电话号码:(\(\d{3,4}\)|\d{3,4}-|\s)?\d{8}
提取信息中的中国电话号码(包括移动和固定电话):(\(\d{3,4}\)|\d{3,4}-|\s)?\d{7,14}
提取信息中的中国邮政编码:[1-9]{1}(\d+){5}
提取信息中的中国身份证号码:\d{18}|\d{15}
提取信息中的整数:\d+
提取信息中的浮点数(即小数):(-?\d*)\.?\d+
提取信息中的任何数字 :(-?\d*)(\.\d+)?
提取信息中的中文字符串:[\u4e00-\u9fa5]*
提取信息中的双字节字符串 (汉字):[^\x00-\xff]*
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值