首页 > 编程语言 >Java正则表达式

Java正则表达式

时间:2024-07-30 18:30:03浏览次数:7  
标签:Java String 正则表达式 System matches println true out

基本规则

image

要点

image
image

Test

	//13112345678    1   [3-9]   \\d{9}
	String regex1 = "1[3-9]\\d{9}";
	System.out.println("13154654456".matches(regex1));//true
	System.out.println("03154654456".matches(regex1));//false
	System.out.println("12154654456".matches(regex1));//false
	System.out.println("1315454456".matches(regex1));//false
	System.out.println("------------------------------------------");

	//0317-4261090 020-2325515
	//区号:   0   \\d{2,3}
	//-  出现0或1次:    -?
	//号码:   [1-9]   \\d{4,9}
	String regex2 = "0\\d{2,3}-?[1-9]\\d{4,9}";
	System.out.println("0317-4264545".matches(regex2));//true
	System.out.println("0174264545".matches(regex2));//true
	System.out.println("1317-4264545".matches(regex2));//false
	System.out.println("017-445".matches(regex2));//false
	System.out.println("0317-0264545".matches(regex2));//false
	System.out.println("------------------------------------------");

	//[email protected] [email protected]
	//@前:任意数字字母下划线至少出现一次 \\w+
	//@到.:任意数字字母出现少于6次[\\w&&[^_]]{2,6}
	//(.   [a-zA-Z]{2,3})   {1,2}:.com/.cn等为一组,出现一到两次
	String regex3 = "\\w+@[\\w&&[^_]]{2,6}(.[a-zA-Z]{2,3}){1,2}";

	System.out.println("[email protected]".matches(regex3));//true
	System.out.println("[email protected]".matches(regex3));//true
	System.out.println("[email protected]".matches(regex3));//true
	System.out.println("[email protected]".matches(regex3));//true
	System.out.println("------------------------------------------");

	//大小写数字下划线4-16位
	String regex4 = "\\w{4,16}";

	//身份证号码简单验证
	String regex5 = "[1-9]\\d{16}(\\d|X|x)";

	//身份证号码简验证
	String regex6 = "[1-9]\\d{5}(18|19|20)\\d{2}(0[1-9]|1[12])(0[1-9]|[12][0-9]|3[01])\\d{3}(\\d|X|x)";

	//忽略大小写
	String regex7 = "a(?i)bc";
	System.out.println("aBC".matches(regex7));//true
	System.out.println("ABC".matches(regex7));//false

插件

  • any_rule

标签:Java,String,正则表达式,System,matches,println,true,out
From: https://www.cnblogs.com/through287/p/18333114

相关文章

  • orc使用java生成文件的示例代码
    包含了int等基本类型、string、数组importorg.apache.hadoop.conf.Configuration;importorg.apache.hadoop.fs.Path;importorg.apache.hadoop.hive.ql.exec.vector.BytesColumnVector;importorg.apache.hadoop.hive.ql.exec.vector.ListColumnVector;importorg.apache.......
  • Java中的变量
    变量目录变量变量的声明变量的分类局部变量成员变量(实例变量)类变量(静态变量)参数变量变量的声明在Java语言中,所有的变量在使用前必须声明。声明变量的基本格式如下:typeidentifier[=value][,identifier[=value]...];格式说明:type--数据类型。identifier--......
  • JavaScript 数据结构与基础算法
    数据结构全解参考:数据结构|博客园-SRIGT相关代码仓库查看:data-struct-js|Github-SR1GT0x00前置知识(1)类使用关键字class声明一个类classPerson{}JavaScript的类中通过constructor使用构建函数classPerson{constructor(name){this.name......
  • java @Cacheable生成的redisKey,出现两个连续的冒号::
    1、参考基于redis2.1.6实现springcache生成的key多出一个冒号2、解决需要对key进行处理,【重点】是computePrefixWith方法config=config.computePrefixWith(cacheName->{returncacheName+StrUtil.COLON;});以下是完整代码实现CacheK......
  • 正则表达式小记
    转义字符在正则表达式中,某些字符具有特殊的含义,它们被称为元字符或特殊字符。当你希望这些特殊字符按照字面意义匹配文本时,就需要使用转义字符(通常是反斜杠\)来“取消”它们的特殊含义。以下是正则表达式中需要转义的常见特殊字符:反斜杠用于转义其他特殊字符或创建预定义字符......
  • Java使用EasyExcel自定义合并(横纵合并)、自定义行高列宽、自适应行高列宽工具Excel导出
    目录一、自适应行高列宽工具类1、自适应行高2、自适应列宽二、自定义行高列宽工具类1、自定义行高2、自定义列宽三、自定义合并工具类四、自定义样式五、实现Excel的完整代码最近又开始写Excel导出的业务,之前写的自适应行高列宽工具类并不满足现下的需求需求是导出......
  • [RoarCTF 2019]Easy Java
    [RoarCTF2019]EasyJavaStep1点击help按钮后发现:URL变成:url/Download?filename=help.docx而回显:java.io.FileNotFoundException:{help.docx}而当我尝试尝试POST,发现文件成功下载:Step2发现可能的漏洞点后,结合WEB-INF相关知识(见文末)可以下载WEB-INF/web.xmlPOST参数......
  • Python正则表达式匹配数字的第一次重复
    示例:For0123123123,1应匹配,因为第二个1出现在任何其他数字重复之前。For01234554321,5应该匹配,因为第二个5出现在任何其他数字的重复之前。我尝试过的一些正则表达式:......
  • javaweb面向切面aop编程-实现自定义填充
    实现自定义填充注解@AutoFill创建annotation包,编写注解类点击查看代码/***自定义注解,用于标识某个方法需要进行功能字段自动填充处理*/@Target(ElementType.METHOD)@Retention(RetentionPolicy.RUNTIME)public@interfaceAutoFill{//数据库操作类型:UPDATEINSE......
  • java编译错误,找不到包的解决办法
    异常:D:\AC2024\20240729\delphiJIN_JAVA\JavaTest\bin\x64>javaMYclasses.JavaClassForDelphiTestExceptioninthread"main"java.lang.NoClassDefFoundError:com/sltas/front/third/util/CryptionUtilatMYclasses.JavaClassForDelphiTest.main(Jav......