共计 5082 个字符,预计需要花费 13 分钟才能阅读完成。
1、正则概述
1.1、什么是正则表达式
正则表达式是一个字符串,使用单个字符串来描述、用来定义匹配规则,匹配一系列符合某个句法规则的字符串。在开发中,正则表达式通常被用来检索、替换那些符合某个规则的文本。
1.2、String 类匹配正则
String 类中,使用正则表达式匹配完整字符串的方法为:
public boolean matches(String regex) 调用方法的字符串为被规则匹配的字符串
regex 字符串(正则字符串)为匹配规则
返回值为布尔型,符合规则返回 true,不符合返回 false
我们简单了解了匹配规则,这里给出手机号与邮箱的匹配规则:
邮箱规则:String regex =”\\w+@\\w{2,7}\\.\\w{2,3}”;
手机号规则:String regex =”[1][3,4,5,7,8][0-9]{9}”;
如:
public class RegexDemo01 {public static void main(String[] args) {String data = "aaa@163.com"; | |
// 返回结果为 true | |
boolean result = data.matches("\\w+@\\w{2,7}\\.\\w{2,3}"); | |
System.out.println(result); | |
String data2 = "itheima.cn"; | |
// 返回结果为 false | |
boolean result2 = data2.matches("\\w+@\\w{2,7}\\.\\w{2,3}"); | |
System.out.println(result2); | |
} | |
} |
运行结果:
案例:
需求:判断一个字符串
(1)长度大于等于 5 小于等于 15
(2)数字开头 不包含 0
正则表达式:”[1-9].{4,14}”
import java.util.Scanner; | |
public class RegexDemo02 {public static void main(String[] args) {Scanner sc = new Scanner(System.in); | |
String s = sc.nextLine(); | |
boolean flag = check(s); | |
boolean flag2 = check2(s); | |
System.out.println(flag); | |
System.out.println(flag2); | |
} | |
public static boolean check2(String s) {// String regex="[1-9].{4,14}"; | |
// boolean b = s.matches(regex); | |
// return b ; | |
return s.matches("[1-9].{4,14}"); | |
} | |
public static boolean check(String s) {boolean flag = true; | |
// 判断长度 | |
if (s.length() >= 5 && s.length() <= 15) {char c = s.charAt(0); | |
// 判断数字开头 不包含 0 | |
if (!(c >= '1' && c <= '9')) {flag = false; | |
} | |
} else {flag = false; | |
} | |
return flag; | |
} | |
} |
运行结果:
1.3、字符串涉及正则常用方法
public String[] split(String regex) // 将符合规则的字符串作为切割符切割
public String replaceAll(String regex,String replacement) // 将符合规则的字符串替换为新字符串
public class RegexDemo03 {public static void main(String[] args) {String girlName = "姗姗. 慧慧. 柳柳. 莲莲. 糖糖"; | |
String[] girlNames = girlName.split("\\."); | |
// System.out.println(girlNames[0]); | |
// System.out.println(girlNames[1]); | |
// System.out.println(girlNames[2]); | |
// System.out.println(girlNames[3]); | |
// System.out.println(girlNames[4]); | |
for (int i = 0; i < girlNames.length; i++) {String string = girlNames[i]; | |
System.out.println(string); | |
} | |
String replaceAll = girlName.replaceAll("柳", "岩岩"); | |
System.out.println(replaceAll); | |
} | |
} |
运行结果:
2、Pattern 类
java.util.regex 是一个用正则表达式所订制的模式来对字符串进行匹配工作的类库包。它包括两个类:Pattern 和 Matcher
2.1、Pattern 类对象创建
Pattern 类用于创建一个正则表达式, 也可以说创建一个匹配模式, 它的构造方法是私有的, 不可以直接创建, 但可以通过 Pattern.complie(String regex) 简单工厂方法创建一个正则表达式
import java.util.regex.Pattern; | |
public class RegexDemo04 {public static void main(String[] args) {Pattern p = Pattern.compile("java*"); | |
System.out.println(p.pattern());// 返回 java* , 返回的既是正则字符串. | |
} | |
} |
pattern() 返回正则表达式的字符串形式, 其实就是返回 Pattern.complile(String regex) 的 regex 参数
2.2、Pattern 常用方法
split(CharSequence input) 方法, 用于分隔字符串, 并返回一个 String[]
import java.util.regex.Pattern; | |
public class RegexDemo05 {public static void main(String[] args) {Pattern p =Pattern.compile("\\d+");//"\\d+" 表示一个或多个数字 | |
String[] str=p.split("a 11123 player 2234 or 345 nothing"); | |
for(int i=0;i<str.length;i++){System.out.println(str[i]); | |
} | |
} | |
} |
运行结果:
Pattern.matches(String regex,CharSequence input) 是一个静态方法, 用于快速匹配字符串, 该方法适合用于只匹配一次, 且匹配全部字符串.
import java.util.regex.Pattern; | |
public class RegexDemo06 {public static void main(String[] args) {boolean b1=Pattern.matches("\\d+","2223");// 返回 true | |
boolean b2=Pattern.matches("\\d+","2223aa");// 返回 false, 需要匹配到所有字符串才能返回 true, 这里 aa 不能匹配到 | |
boolean b3=Pattern.matches("\\d+","22bb23");// 返回 false, 需要匹配到所有字符串才能返回 true, 这里 bb 不能匹配到 | |
System.out.println(b1); | |
System.out.println(b2); | |
System.out.println(b3); | |
} | |
} |
运行结果:
3、Matcher 类
Matcher 一个 Matcher 对象是一个状态机器,它依据 Pattern 对象做为匹配模式对字符串展开匹配检查。
3.1、Matcher 类对象创建
Matcher 类构造方法也是私有的, 不能直接创建, 只能通过 Pattern.matcher(CharSequence input) 方法得到该类的实例
import java.util.regex.Matcher; | |
import java.util.regex.Pattern; | |
public class RegexDemo07{public static void main(String[] args) {Pattern p = Pattern.compile("\\d+"); | |
Matcher m = p.matcher("22bb23"); | |
System.out.println(m.pattern());// 返回 \d+ | |
} | |
} |
运行结果:
3.2、Matcher 类常用方法
matches(): 对 整个字符串进行匹配, 只有整个字符串都匹配了才返回 true
import java.util.regex.Matcher; | |
import java.util.regex.Pattern; | |
public class RegexDemo08 {public static void main(String[] args) {Pattern pm = Pattern.compile("\\d+"); | |
Matcher mm = pm.matcher("1111a"); | |
System.out.println(mm.matches());// 因为 a 不能被 \d+ 匹配, 返回 false | |
Matcher mm2 = pm.matcher("1111"); | |
System.out.println(mm2.matches());// 返回 true, 因为 \d+ 匹配到了整个字符串 | |
} | |
} |
运行结果:
lookingAt(): 对前面的字符串进行匹配, 只有在最前面匹配到才返回 true
import java.util.regex.Matcher; | |
import java.util.regex.Pattern; | |
public class RegexDemo09 {public static void main(String[] args) {Pattern pl = Pattern.compile("\\d+"); | |
Matcher ml = pl.matcher("1111a"); | |
System.out.println(ml.lookingAt());// 返回 true, 因为 \d+ 匹配到了前面的 1 | |
Matcher ml2 = pl.matcher("a1111"); | |
System.out.println(ml2.lookingAt());// 返回 false, 因为 \d+ 不能匹配前面的 a | |
} | |
} |
运行结果:
find(): 对字符串进行匹配, 匹配到的字符串可以在任何位置.
import java.util.regex.Matcher; | |
import java.util.regex.Pattern; | |
public class RegexDemo10 {public static void main(String[] args) {Pattern pf = Pattern.compile("\\d+"); | |
Matcher mf = pf.matcher("1111a"); | |
System.out.println(mf.find());// 返回 true | |
Matcher mf2 = pf.matcher("aaaa"); | |
System.out.println(mf2.find());// 返回 false | |
} | |
} |
运行结果:
当使用 matches(),lookingAt(),find() 执行匹配操作后, 就可以利用以上三个方法得到更详细的信息.
start() 返回匹配到的子字符串在字符串中的索引位置.
end() 返回匹配到的子字符串的最后一个字符在字符串中的索引位置.
group() 返回匹配到的子字符串
import java.util.regex.Matcher; | |
import java.util.regex.Pattern; | |
public class RegexDemo11{public static void main(String[] args) {Pattern p=Pattern.compile("\\d+"); | |
Matcher m=p.matcher("aaa2223bb"); | |
System.out.println(m.find());// 匹配 2223 | |
System.out.println(m.start());// 返回 3 | |
System.out.println(m.end());// 返回 7, 返回的是 2223 后的索引号 | |
System.out.println(m.group());// 返回 2223 | |
} | |
} |
运行结果:
