RegExUtil

大家看看,有需要的拿走。


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

public final class RegExUtil {
	public final static String EMAIL = "(?i)^[_a-z0-9\\-]+([._a-z0-9\\-]+)*@[a-z0-9\\-]+([.a-z0-9\\-]+)*(\\.[a-z]{2,4})$";
	
	public final static String NUMBER = "[+-]?\\d+[.0-9]*";
	
	public final static String URL = "(?i)^(http|https|www|ftp|)?(://)?([_a-z0-9\\-].)+[_a-z0-9\\-]+(\\/[_a-z0-9\\-])*(\\/)*([_a-z0-9\\-].)*([_a-z0-9\\-&#?=])*$";
	
	public final static String CASE = "^[a-zA-Z]+$";
	
	public final static String CHINESE = "[\\u4e00-\\u9fa5]*";
	
	public final static String TWOBYTES = "[^\\x00-\\xff]";
	
	public final static String IDCARD = "^(\\d{6})(\\d{4})(\\d{2})(\\d{2})(\\d{3})([0-9]|X)$";
	
	/**
	 * 手机号码
	 * 移动:134[0-8],135,136,137,138,139,150,151,157,158,159,182,187,188
	 * 联通:130,131,132,152,155,156,185,186
	 * 电信:133,1349,153,180,189
	 */
	public final static String MOBILE = "^0?(13[0-9]|15[012356789]|18[0236789]|14[57])[0-9]{8}";
	/**
	 * 中国移动:China Mobile
	 * 134[0-8],135,136,137,138,139,150,151,157,158,159,182,187,188
	 */
	public final static String MOBILE_CM = "^1(34[0-8]|(3[5-9]|5[017-9]|8[278])\\d)\\d{7}$";
	/**
	 * 中国联通:China Unicom
	 * 130,131,132,152,155,156,185,186
	 */
	public final static String MOBILE_CU = "^1(3[0-2]|5[256]|8[56])\\d{8}$";
	/**
	 * 中国电信:China Telecom
	 * 133,1349,153,180,189
	 */
	public final static String MOBILE_CT = "^1((33|53|8[09])[0-9]|349)\\d{7}$";
	/**
	 * 大陆地区固话及小灵通
	 * 区号:010,020,021,022,023,024,025,027,028,029
	 * 号码:七位或八位
	 */
	public final static String MOBILE_PHS = "^0(10|2[0-5789]|\\d{3})\\d{7,8}$";
	
	private RegExUtil() {
	}

	public static List<String> findAll(final String source, final String regex) {
		final List<String> result = new ArrayList<String>();
		final Matcher matcher = genMatcher(regex, source, null);
		while (matcher.find()) {
			result.add(matcher.group());
		}
		return result;
	}

	public static String findFirst(final String source, final String regex) {
		final Matcher matcher = genMatcher(regex, source, null);
		return matcher.find() ? matcher.group() : "";
	}

	public static String findAndReplace(final String source, final String regex, final String replacement) {
		final Matcher matcher = genMatcher(regex, source, null);
		return matcher.find() ? source.replace(matcher.group(), replacement) : source;
	}
	
	public static Matcher genMatcher(final String source, final String regex, final Integer flag) {
		return Pattern.compile(regex, (flag == null) ? 0 : flag).matcher(source);
	}
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值