What is the usage of java regular expressions
正则表达式是一种可以用于模式匹配和替换的规范,一个正则表达式就是由普通的字符(例如字符a到z)以及特殊字符(元字符)组成的文字模式,它 用以描述在查找文字主体时待匹配的一个或多个字符串。正则表达式作为一个模板,将某个字符模式与所搜索的字符串进行匹配。
众所周知,在程序开发中,难免会遇到需要匹配、查找、替换、判断字符串的情况发生,而这些情况有时又比较复杂,如果用纯编码方式解决,往往会浪费程序员的时间及精力。因此,学习及使用正则表达式,便成了解决这一矛盾的主要手段。
大家都知道,正则表达式是一种可以用于模式匹配和替换的规范,一个正则表达式就是由普通的字符(例如字符a到z)以及特殊字符(元字符)组成的文字模式,它 用以描述在查找文字主体时待匹配的一个或多个字符串。正则表达式作为一个模板,将某个字符模式与所搜索的字符串进行匹配。
自从jdk1.4推出java.util.regex包,就为我们提供了很好的JAVA正则表达式应用平台。
因为正则表达式是一个很庞杂的体系,所以我仅例举些入门的概念,更多的请参阅相关书籍及自行摸索。
*下面是java中正则表达式常用的语法:
字符的取值范围
1.[abc] : 表示可能是a,可能是b,也可能是c。
2.[^abc]: 表示不是a,b,c中的任意一个
3.[a-zA-Z]: 表示是英文字母
4.[0-9]:表示是数字
简洁的字符表示
.:匹配任意的字符
\d:表示数字
\D:表示非数字
\s:表示由空字符组成,[ \t\n\r\x\f]
\S:表示由非空字符组成,[^\s]
\w:表示字母、数字、下划线,[a-zA-Z0-9_]
\W:表示不是由字母、数字、下划线组成
数量表达式
1.?: 表示出现0次或1次
2.+: 表示出现1次或多次
3.*: 表示出现0次、1次或多次
4.{n}:表示出现n次
5.{n,m}:表示出现n~m次
6.{n,}:表示出现n次或n次以上
逻辑表达式
1.XY: 表示X后面跟着Y,这里X和Y分别是正则表达式的一部分
2.X|Y:表示X或Y,比如"food|f"匹配的是foo(d或f),而"(food)|f"匹配的是food或f
3.(X):子表达式,将X看做是一个整体
java中提供了两个类来支持正则表达式的操作
分别是java.util.regex下的Pattern类和Matcher类
使用Pattern类进行字符串的拆分,使用的方法是String[] split(CharSequence input)
使用Matcher类进行字符串的验证和替换,
匹配使用的方法是boolean matches()
替换使用的方法是 String replaceAll(String replacement)
Pattern类的构造方法是私有的
所以我们使用Pattern p = Pattern.compile("a*b");进行实例化
Matcher类的实例化依赖Pattern类的对象Matcher m = p.matcher("aaaaab");
在实际的开发中,为了方便我们很少直接使用Pattern类或Matcher类,而是使用String类下的方法
验证:boolean matches(String regex)
拆分: String[] split(String regex)
替换: String replaceAll(String regex, String replacement)
下面是正则表达式的简单使用:
1、Test01.java :使用正则表达式使代码变得非常简洁。
package test_regex; public class Test01 { public static void main(String[] args){ String str = "1234567"; // char[] c = str.toCharArray(); // boolean b = true; // for(char c1:c){ // if(!(c1>='0'&&c1<='9')){ // b = false; // break; // } // } // System.out.println(b); String regex = "\\d+"; System.out.println(str.matches(regex)); } }
2、TestMatcher01.java(Matcher类的使用,用于字符串的验证)
package test_regex; import java.util.regex.Pattern; import java.util.regex.Matcher; public class TestMatcher01 { public static void main(String[] args){ String str = "1234567abc"; String regex = "\\w{10,}"; // Pattern pat = Pattern.compile(regex); // Matcher mat = pat.matcher(str); // System.out.println(mat.matches()); System.out.println(str.matches(regex)); } }
3、TestMatcher02.java(Matcher类的使用,用于字符串的替换)
package test_regex; import java.util.regex.Pattern; import java.util.regex.Matcher; public class TestMatcher02 { public static void main(String[] args){ String str = "12Y34h56dAd7"; String regex = "[a-zA-Z]+"; // Pattern pat = Pattern.compile(regex); // Matcher mat = pat.matcher(str); // System.out.println(mat.replaceAll(":")); System.out.println(str.replaceAll(regex,"-")); } }
4、TestPattern01.java(Pattern类的使用,用于字符串的拆分)
package test_regex; import java.util.regex.Pattern; public class TestPattern01 { public static void main(String[] args){ String str = "Tom:30|Jerry:20|Bob:25"; String regex = "\\|"; // Pattern pat = Pattern.compile(regex); // String[] arr = pat.split(str); String[] arr = str.split(regex); for(String s:arr){ System.out.println(s); } } }
5、TestRegex01.java(大概判断一个邮箱地址是否合法)
package test_regex; public class TestRegex01 { //判断一个邮箱地址是否合法 public static void main(String[] args){ //这里默认邮箱的后缀是.com或.net.cn String str = "aa@aa.net.cn"; String regex = "\\w+@\\w+\\.(com|net.cn)"; System.out.println(str.matches(regex)); } }
推荐教程: 《java教程》
The above is the detailed content of What is the usage of java regular expressions. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Guide to Square Root in Java. Here we discuss how Square Root works in Java with example and its code implementation respectively.

Guide to Perfect Number in Java. Here we discuss the Definition, How to check Perfect number in Java?, examples with code implementation.

Guide to the Armstrong Number in Java. Here we discuss an introduction to Armstrong's number in java along with some of the code.

Guide to Random Number Generator in Java. Here we discuss Functions in Java with examples and two different Generators with ther examples.

Guide to Weka in Java. Here we discuss the Introduction, how to use weka java, the type of platform, and advantages with examples.

Guide to Smith Number in Java. Here we discuss the Definition, How to check smith number in Java? example with code implementation.

In this article, we have kept the most asked Java Spring Interview Questions with their detailed answers. So that you can crack the interview.

Java 8 introduces the Stream API, providing a powerful and expressive way to process data collections. However, a common question when using Stream is: How to break or return from a forEach operation? Traditional loops allow for early interruption or return, but Stream's forEach method does not directly support this method. This article will explain the reasons and explore alternative methods for implementing premature termination in Stream processing systems. Further reading: Java Stream API improvements Understand Stream forEach The forEach method is a terminal operation that performs one operation on each element in the Stream. Its design intention is
