我们在日常的Java开发中,经常需要处理一些字符串,这个时候正则表达式是非常有用的。几乎在所有的编程语言中都支持正则表达式。以下我将压箱底多年的干货搬出来给大家参考,都是我们日常使用频次比较高的正则表达式,希望能能大大提高你的工作效率。
- 整数
public static final String intege = "^-?[1-9]\\d*$/"; //整数
/** 正例 */
System.out.println(Pattern.matches(intege,"123")); // true
System.out.println(Pattern.matches(intege,"-123")); // true
/** 反例 */
System.out.println(Pattern.matches(intege,"abc")); // false
System.out.println(Pattern.matches(intege,"0")); // false
- 正整数
public static final String intege1 = "^[1-9]\\d*$/"; //正整数
// 正例
System.out.println(Pattern.matches(intege1,"123")); // true
// 反例
System.out.println(Pattern.matches(intege1,"-123")); // false
System.out.println(Pattern.matches(intege1,"0")); // false
- 负整数
public static final String intege2 = "^-[1-9]\\d*$/"; //负整数
// 正例
System.out.println(Pattern.matches(intege2,"-123")); // true
// 反例
System.out.println(Pattern.matches(intege2,"123")); // false
System.out.println(Pattern.matches(intege2,"0")); // false
- 数字
public static final String num = "^([+-]?)\\d*\\.?\\d+$/"; //数字
// 正例
System.out.println(Pattern.matches(num,"123")); // true
System.out.println(Pattern.matches("0")); // true
// 反例
System.out.println(Pattern.matches(num,"a123")); // false
- 正数(正整数 + 0)
public static final String num1 = "^[1-9]\\d*|0$/"; //正数(正整数 + 0)
// 正例
System.out.println(Pattern.matches(num1,"123")); // true
System.out.println(Pattern.matches(num1,"0")); // true
// 反例
System.out.println(Pattern.matches(num1,"-123")); // false
- 负数(负整数 + 0)
public static final String num2 = "^-[1-9]\\d*|0$/"; //负数(负整数 + 0)
// 正例
System.out.println(Pattern.matches(num2,"-123")); // true
System.out.println(Pattern.matches(num2,"0")); // true
// 反例
System.out.println(Pattern.matches(num2,"123")); // false
- 浮点数
public static final String decmal = "^([+-]?)\\d*\\.\\d+$/"; //浮点数
// 正例
System.out.println(Pattern.matches(decmal,"-0.1")); // true
System.out.println(Pattern.matches(decmal,"0.1")); // true
// 反例
System.out.println(Pattern.matches(decmal,"a.b")); // false
- 邮箱地址
public static final String email = "^\\w+((-\\w+)|(\\.\\w+))*\\@[A-Za-z0-9]+((\\.|-)[A-Za-z0-9]+)*\\.[A-Za-z0-9]+$"; //邮件
// 正例
System.out.println(Pattern.matches(email,"1111@qq.com")); // true
// 反例
System.out.println(Pattern.matches(email,"11.qq.com")); // false
- url匹配
public static final String url = "^http[s]?:\\/\\/([\\w-]+\\.)+[\\w-]+([\\w-.\\/?%&=]*)?$"; //url
// 正例
System.out.println(Pattern.matches(url,"http://www.xxx.com")); // true
System.out.println(Pattern.matches(url,"https://www.xxx.com")); // true
System.out.println(Pattern.matches(url,"www.xxx.com")); // true
// 反例
System.out.println(Pattern.matches(url,"abcd")); // false
- 纯仅中文字符
public static final String chinese = "^[\\u4E00-\\u9FA5\\uF900-\\uFA2D]+$"; //仅中文
// 正例
System.out.println(Pattern.matches(chinese,"架构")); // true
// 反例
System.out.println(Pattern.matches(chinese,"cc架构")); // false
- 仅ACSII字符
public static final String ascii = "^[\\x00-\\xFF]+$"; //仅ACSII字符
// 正例
System.out.println(Pattern.matches(ascii,"abc123")); // true
// 反例
System.out.println(Pattern.matches(ascii,"にそ①②③")); // false
- 邮政编码
public static final String zipcode = "^\\d{6}$"; //邮编
// 正例
System.out.println(Pattern.matches(zipcode,"100000")); // true
// 反例
System.out.println(Pattern.matches(zipcode,"1000000")); // false
- 国内手机号码
public static final String mobile = "^(13|15|16|17|18)[0-9]{9}$"; //手机
// 正例
System.out.println(Pattern.matches(zipcode,"13800138000")); // true
// 反例
System.out.println(Pattern.matches(zipcode,"19900010002")); // false
- 非空字符
public static final String notempty = "^\\S+$"; //非空
// 正例
System.out.println(Pattern.matches(notempty," abc ")); // true
// 反例
System.out.println(Pattern.matches(notempty,"")); // false
- 图片后缀
public static final String picture = "(.*)\\.(jpg|bmp|gif|ico|pcx|jpeg|tif|png|raw|tga|JPG|BMP|GIF|ICO|PCX|JPEG|TIF|PNG|RAW|TGA)$"; //图片
// 正例
System.out.println(Pattern.matches(picture,"cc.jpg")); // true
// 反例
System.out.println(Pattern.matches(picture,"cc.txt"")); // false
- 日期格式
public static final String date = "^\\d{4}(\\-|\\/|\\.)\\d{1,2}\\1\\d{1,2}$"; //日期
// 正例
System.out.println(Pattern.matches(date,"2024-10-24")); // true
System.out.println(Pattern.matches(date,"2024/10/24")); // true
// 反例
System.out.println(Pattern.matches(date,"2024年10月24日"")); // false
- 纯字母
public static final String letter = "^[A-Za-z]+$"; //字母
// 正例
System.out.println(Pattern.matches(letter,"abc")); // true
// 反例
System.out.println(Pattern.matches(letter,"abc123")); // false
- 身份证号码
public static final String idcard = "^[1-9]([0-9]{14}|[0-9]{17})$"; //身份证
// 正例
System.out.println(Pattern.matches(idcard,"100000201410241024")); // true
// 反例
System.out.println(Pattern.matches(idcard,"1000002014102410240")); // false
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之家整理,本文链接:https://www.bmabk.com/index.php/post/16853.html