Java基本正则表达式

news/2024/7/2 0:51:30 标签: java, 正则表达式

文章目录

    • Java 正则表达式
      • 匹配单个字符
      • 匹配多个字符
      • 匹配0次或者一次
      • 匹配数字或者非数字
      • 匹配任何字符类字符0-9 a-z A-Z
      • 或者
      • 匹配中文字符
      • 匹配空白字符
      • 任意字符

本文主要介绍java正则表达式的基本用法,常用的匹配。

Java 正则表达式

正则表达式定义了字符串的模式。
正则表达式可以用来搜索、编辑或处理文本。
正则表达式并不仅限于某一种语言,但是在每种语言中有细微的差别。

下面为案列及代码。

注意:
matches方法中,() 中双引号中的内容为正则表达式

匹配单个字符

java">String str1 = "a";
//只匹配字符a
System.out.println(str1.matches("a"));//TRUE

//匹配0-9,a-z,A-Z 的单个字符
System.out.println(str1.matches("[a-z0-9A-Z]"));//TRUE

//^在[]里面代表非 , 匹配不是0-9,a-z,A-Z 的单个字符
System.out.println(str1.matches("[^a-z0-9A-Z]"));//false

匹配多个字符

java">String str2 = "java";
//直接匹配相同的字符串
System.out.println(str2.matches("java"));//true
//一个字符一个字符的匹配
System.out.println(str2.matches("[a-z0-9A-Z][a-z0-9A-Z][a-z0-9A-Z][a-z0-9A-Z]"));//true
//{}代表几次
System.out.println(str2.matches("[a-z0-9A-Z]{4}"));//true
//+代表一次或者多次
System.out.println(str2.matches("[a-z0-9A-Z]+"));//true
System.out.println("".matches("[a-z0-9A-Z]+"));//false
//*表示0次或者多次
System.out.println(str2.matches("[a-z0-9A-Z]*"));//true
System.out.println("".matches("[a-z0-9A-Z]*"));//true

匹配0次或者一次

java">String str3 = "abc";
//匹配3-4个字符
System.out.println(str3.matches("[a-z0-9A-Z][a-z0-9A-Z][a-z0-9A-Z][a-z0-9A-Z]?"));//true
System.out.println("abcde".matches("[a-z0-9A-Z][a-z0-9A-Z][a-z0-9A-Z][a-z0-9A-Z]?"));//false

匹配数字或者非数字

注意:
此时使用 \ \ 是因为 \ 为转义

java">String str4 = "123";
String str5 = "abc";
//匹配数字  /d
System.out.println(str4.matches("\\d+"));//true
System.out.println(str5.matches("\\d+"));//false
//匹配非数字
System.out.println(str4.matches("\\D+"));//false
System.out.println(str5.matches("\\D+"));//true

匹配任何字符类字符0-9 a-z A-Z

java">String str6 = "123";
String str7 = "abc";
String str8 = "%^&";
//\w 匹配 0-9 a-z A-Z
System.out.println(str6.matches("\\w+"));//TRUE
System.out.println(str7.matches("\\w+"));//TRUE
System.out.println(str8.matches("\\w+"));//FALSE
//\W 匹配非 0-9 a-z A-Z
System.out.println(str6.matches("\\W+"));//FALSE
System.out.println(str7.matches("\\W+"));//FALSE
System.out.println(str8.matches("\\W+"));//TRUE

或者

java">String sex1 = "男";
String sex2 = "女";
String sex3 = "男女";
//|前后代表或者的关系 , 一般匹配多个字符时 带()即可
System.out.println(sex1.matches("[男]|[女]"));//true
System.out.println(sex2.matches("[男]|[女]"));//true
System.out.println(sex3.matches("[男]|[女]"));//true

匹配中文字符

java">String str9 = "我爱中国";
String str10 = "ilovechina";
//匹配中文 \u4e00-\u9fa5 为unicode 编码范围
System.out.println(str9.matches("[\u4e00-\u9fa5]+"));//true
System.out.println(str10.matches("[\u4e00-\u9fa5]+"));//false
//匹配非中文字符
System.out.println(str9.matches("[^\u4e00-\u9fa5]+"));//false
System.out.println(str10.matches("[^\u4e00-\u9fa5]+"));//true

匹配空白字符

String str11 = "abc";
String str12 = " 	";//空格 和 制表符
// \s 匹配空白字符
System.out.println(str11.matches("[\\s]+"));//false
System.out.println(str12.matches("[\\s]+"));//true
// \S 匹配非空白字符
System.out.println(str11.matches("[\\S]+"));//true
System.out.println(str12.matches("[\\S]+"));//false

任意字符

· 除了\n外的其他字符
可以使用 \w\W 或者 \D\d 等其他类似的方式来匹配任意字符

注意:

代码中出现" \ \ ",均为’’,因为java中\的作用是用来做转义字符使用的


http://www.niftyadmin.cn/n/1441002.html

相关文章

打造WinXP系统万能克隆Ghost全攻略(转)

一直以来,安装操作系统和应用软件是一件吃力不讨好的事情,虽然现在的电脑速度越 来越快,并且操作系统安装步骤也很简单,但每次都是只能等系统慢慢地一步步完成,系统装完后,接着装应用软件,并具还…

c# async 事物

上菜 async await 机制 确实便捷开发 多线程时 如何一致性如何保证呢&#xff1f; public async Task<ActionResult<IEnumerable<string>>> Get(){using (var trans new TransactionScope(TransactionScopeAsyncFlowOption.Enabled)){try{Console.WriteL…

WebRTC网关服务器单端口方案实现

标准WebRTC连接建立流程这里描述的是Trickle ICE过程&#xff0c;并且省略了通话发起与接受的信令部分。流程如下&#xff1a;1&#xff09; WebRTC A通过Signal Server转发SDP OFFER到WebRTC B。WebRTC B做完本地处理以后&#xff0c;通过 Signal Server转发SDP ANSWER到A。2&…

基数排序如何实现

文章目录基数排序简介实现原理基本解法&#xff1a;举例&#xff1a;java代码基数排序 简介 基数排序&#xff08;radix sort&#xff09;属于“分配式排序”&#xff08;distribution sort&#xff09;&#xff0c;又称“桶子法”&#xff08;bucket sort&#xff09;或bin …

(三) 创建Hello-Service提供者 - Spring Cloud复习笔记

&#xff08;三&#xff09; 创建Hello-Service提供者 - Spring Cloud学习笔记 1、核心技术选型&#xff1a; Maven&#xff1a;3.3.9  JDK&#xff1a; 1.8.0_144  spring-boot-starter-parent&#xff1a; 2.0.3.RELEASE  spring-cloud-dependencies&#xff1a; Finch…

从Alexa网站排名,看IT门户走势(转)

经常有人拿 Alexa 网站提供的全球网站排名服务&#xff0c;与《财富》的世界 500 强企业排名及《福布斯》全球富豪排名等相提并论&#xff0c;称其为第三方全球排名权威机构。 Alexa 网站所提供的关于网站的动态数据、流量信息、质量分析等高质量服务&#xff0c;再加上与 goog…

掌握react,这一篇就够了

react众所周知的前端3大主流框架之一&#xff0c;由于出色的性能&#xff0c;完善的周边设施风头一时无两。本文就带大家一起掌握react。 jsx语法 前端MVVM主流框架都有一套自己的模板处理方法&#xff0c;react则使用它独特的jsx语法。在组件中插入html类似的语法&#xff0c;…

对象与类的简介

文章目录前言类对象前言 面向对象程序设计&#xff08;简称OOP&#xff09;是当今主流的程序设计范型&#xff0c;它已经取代了20世纪70年代的“结构化”过程化程序设计开发技术。Java是完全面向对象的&#xff0c;必须熟悉OOP才能够编写Java程序。 面向对象的程序是由对象组…