android 正则表达式 实例

网上有很多正则表达式的规则之类的,就不发了,我也是在根据规则简单的写了点自己可能用到的一些,直接上代码实例,以下方法都是android自带的

/**
	 * 字符串是否为车牌号
	 * @param s
	 * @return
	 */
	public static boolean isPlate(String s)
	{
		if(s==null)
		{
			return false;
		}
		Pattern pattern = Pattern.compile("[\u4E00-\u9FA5][a-zA-Z][a-zA-Z0-9]{5}");
		Matcher matcher = pattern.matcher(s);
		return matcher.matches();
	}
	
	
	/**
	 * 字符串是否为纯数字
	 * @param s
	 * @return
	 */
	public static boolean isNumOnly(String s)
	{
		if(s==null)
		{
			return false;
		}
		Pattern pattern = Pattern.compile("[0-9]+");
		Matcher matcher = pattern.matcher(s);
		return matcher.matches();
	}
	
	/**
	 * 是否含有数字
	 * @param s
	 * @return
	 */
	public static boolean hasNum(String s)
	{
		if(s==null)
		{
			return false;
		}
		Pattern pattern = Pattern.compile(".*[0-9].*");
		Matcher matcher = pattern.matcher(s);
		return matcher.matches();
	}
	
	/**
	 * 除去字符串中所有的数字
	 * @param s
	 * @return
	 */
	public static String removeNums(String s)
	{
		if(s==null)
		{
			return "";
		}
		Pattern ptn = Pattern.compile("[0-9]+");
	    Matcher m = ptn.matcher(s);
	    return m.replaceAll("");
	}
	
	/**
	 * 只保留数字
	 * @param s
	 * @return
	 */
	public static String retainNumsOnly(String s)
	{
		if(s==null)
		{
			return "";
		}
		Pattern ptn = Pattern.compile("[^0-9]+");
	    Matcher m = ptn.matcher(s);
	    return m.replaceAll("");
	}
	
	/**
	 * 除去字符串中所有的汉字
	 * @param s
	 * @return
	 */
	public static String removeChn(String s)
	{
		if(s==null)
		{
			return "";
		}
		Pattern ptn = Pattern.compile("[\u4E00-\u9FA5]+");
	    Matcher m = ptn.matcher(s);
	    return m.replaceAll("");
	}
	
	/**
	 * 大写转小写(除去不相关字符)
	 * @return
	 */
	public static String Aa(String s)
	{
		if(s==null)
		{
			return "";
		}
		Pattern ptn = Pattern.compile("[^a-zA-Z]+");
	    Matcher m = ptn.matcher(s);
	    String ss=m.replaceAll("");
	    return ss.toLowerCase();
	}
	
	/**
	 * 小写转大写(除去不相关字符)
	 * @return
	 */
	public static String aA(String s)
	{
		if(s==null)
		{
			return "";
		}
		Pattern ptn = Pattern.compile("[^a-zA-Z]+");
	    Matcher m = ptn.matcher(s);
	    String ss=m.replaceAll("");
	    return ss.toUpperCase();
	}
	
	/**
	 * 把小写字母转为大写字母(不出去不相关字符)
	 * @param s
	 * @return
	 */
	public static String upperLetter(String s)
	{
		if(s==null)
		{
			return "";
		}
		return s.toUpperCase();
	}
	
	/**
	 * 把大写字母转为小写字母(不出去不相关字符)
	 * @param s
	 * @return
	 */
	public static String lowerLetter(String s)
	{
		if(s==null)
		{
			return "";
		}
		return s.toLowerCase();
	}
}

上面都是自己一行行打的 有错误请指正
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值