一、精确查找

假设仅仅查找hi俩字母
很多单词里包含hi这两个连续的字符,比如him,history,high等等。用hi来查找的话,这里边的hi也会被找出来。如果要精确地查找hi这个单词的话,我们应该使用**"\bhi\b"。**

\b
匹配一个字边界,即字与空格间的位置。例如,“er\b"匹配"never"中的"er”,但不匹配"verb"中的"er"。

二、正则匹配电话号码

从一个字符串中查找出类似
010-89153248
0431-4512742
(010)-89153248
(0431)-4512742

* 	"\D" 非数字字符匹配。等效于 [^0-9]		\\转义
	 * 	"\d" 数字字符匹配。 等效[0-9]
	 * 	"." 匹配任意字符
	 * 	"*" 零次或多次匹配前面的字符或子表达式
	 * 	"?" 零次或多次匹配前面...
	 * 	"+" 一次或多次匹配...
	 * 	"\d{2-5}" 2到5位纯数字

Java对于"\"的处理特殊
在其他语言中,\\ 表示:我想要在正则表达式中插入一个普通的(字面上的)反斜杠,请不要给它任何特殊的意义。
在 Java 中,\\ 表示:我要插入一个正则表达式的反斜线,所以其后的字符具有特殊的意义。
【其他语言单倍,Java双倍】
所以,在其他的语言中(如Perl),一个反斜杠 \ 就足以具有转义的作用,而在 Java 中正则表达式中则需要有两个反斜杠才能被解析为其他语言中的转义作用。也可以简单的理解在 Java 的正则表达式中,两个 \\ 代表其他语言中的一个 \,这也就是为什么表示一位数字的正则表达式是 \\d,而表示一个普通的反斜杠是 \\\\。

包含0开头3位区号-8位电话号码,或 0开头4位区号-7位电话号码

System.out.println("\n==包含0开头3位区号-8位电话号码,或 0开头4位区号-7位电话号码==");
		String contest = "what 452 the f***" + "how are you ? " + "fine.com" + "010-89153248";
		String pattern = ".*0\\d{2}-\\d{8}.*|.*0\\d{3}-\\d{7}.*";	
		System.out.println(contest);
		boolean isMatch = Pattern.matches(pattern, contest);
		System.out.println("匹配吗?"+isMatch);

包含0开头3位区号(可带小括号)-8位电话号码,或 0开头4位区号-7位电话号码

System.out.println("\n==包含0开头3位区号(可带小括号)-8位电话号码,或 0开头4位区号-7位电话号码==");
		contest = "what 452 the f***" + "how are you ? " + "fine.com" + "(010)-89153248";
		pattern = ".*\\(?0\\d{2}\\)?-\\d{8}.*|.*\\)0\\d{3}\\)?-\\d{7}.*";	
		System.out.println(contest);
		isMatch = Pattern.matches(pattern, contest);
		System.out.println("匹配吗?"+isMatch);

三、正则分组

分组找出数字电话号码, contest整体算第一组

System.out.println("==分组找出数字, contest本身算第一组==");
		String contest = "This order was placed for QT3000! OK?";
		String pattern = "(\\D*)(\\d+)(.*)";	
		// 创建Pattern对象, 编译给定的正则表达式
		Pattern ptn = Pattern.compile(pattern);
		// 创建Match对象
		Matcher mtc = ptn.matcher(contest);
		if (mtc.find()) {
			for (int i = 0; i < mtc.groupCount()+1; i++) {
				System.out.println(mtc.group(i));
			}
		}else {
			System.out.println("no match");
		}
		
		System.out.println("\n==包含0开头3位区号-8位电话号码,或 0开头4位区号-7位电话号码==");
		contest = "what 452 the f***" + "how are you ? " + "fine.com" + "010-89153248";
		pattern = ".*0\\d{2}-\\d{8}.*|.*0\\d{3}-\\d{7}.*";	
		System.out.println(contest);
		boolean isMatch = Pattern.matches(pattern, contest);
		System.out.println("匹配吗?"+isMatch);
  • 本文仅供参考,可粘贴至本地IDE测试
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class demo {
	public static void main(String[] args) {
		/*
		 * 	"\D" 非数字字符匹配。等效于 [^0-9]		\\转义
		 * 	"\d" 数字字符匹配。 等效[0-9]
		 * 	"." 匹配任意字符
		 * 	"*" 零次或多次匹配前面的字符或子表达式
		 * 	"?" 零次或多次...
		 * 	"+" 一次或多次...
		 * 	"\d{2-5}" 2到5位纯数字
		 * */
		System.out.println("==分组找出数字, contest本身算第一组==");
		String contest = "This order was placed for QT3000! OK?";
		String pattern = "(\\D*)(\\d+)(.*)";	
		// 创建Pattern对象, 编译给定的正则表达式
		Pattern ptn = Pattern.compile(pattern);
		// 创建Match对象
		Matcher mtc = ptn.matcher(contest);
		if (mtc.find()) {
			for (int i = 0; i < mtc.groupCount()+1; i++) {
				System.out.println(mtc.group(i));
			}
		}else {
			System.out.println("no match");
		}
		
		System.out.println("\n==包含0开头3位区号-8位电话号码,或 0开头4位区号-7位电话号码==");
		contest = "what 452 the f***" + "how are you ? " + "fine.com" + "010-89153248";
		pattern = ".*0\\d{2}-\\d{8}.*|.*0\\d{3}-\\d{7}.*";	
		System.out.println(contest);
		boolean isMatch = Pattern.matches(pattern, contest);
		System.out.println("匹配吗?"+isMatch);
		
		System.out.println("\n==包含0开头3位区号(可带小括号)-8位电话号码,或 0开头4位区号-7位电话号码==");
		contest = "what 452 the f***" + "how are you ? " + "fine.com" + "(010)-89153248";
		pattern = ".*\\(?0\\d{2}\\)?-\\d{8}.*|.*\\)0\\d{3}\\)?-\\d{7}.*";	
		System.out.println(contest);
		isMatch = Pattern.matches(pattern, contest);
		System.out.println("匹配吗?"+isMatch);
		
		System.out.println("\n==============================");
		contest = "what 452 the f***" + "how are you ? " + "fine.com" + "(010)-89153248";
		pattern = "(\\D*)(\\(0\\d{2}\\)-\\d{8})";	//正则匹配
		System.out.println(contest);
		
		ptn = Pattern.compile(pattern);
		mtc = ptn.matcher(contest);
		if (mtc.find()) {
			for (int i = 0; i < mtc.groupCount()+1; i++) {
				System.out.println(mtc.group(i));
			}

		}else {
			System.out.println("no match");
		}
		

	}
	
	

}

输出结果

String java 正则搜索 java正则查找_System

<!--参考网址-->
<a href>https://deerchao.cn/tutorials/regex/regex.htm</a>

四、反义

反义,即需要查找不属于某个能简单定义的字符类的字符。

code

comment

\W

匹配任意不是字母,数字,下划线,汉字的字符

\S

匹配任意不是空白符的字符

\D

匹配任意非数字的字符

\B

匹配不是单词开头或结束的位置

[^x]

匹配除了x以外的任意字符

[^aeiou]

匹配除了aeiou这几个字母以外的任意字符

例子:
查找<K****>尖括号内K开头单词*代表一个以上字符**

System.out.println("\n=========查找<K******>尖括号内K开头单词*代表一个以上字符======================");
		contest = "what 452 the f***" + "<dsK>"+"<KalStudio>"+ "(010)-89153248";
		pattern = ".*<K[^>]+>.*";	
		System.out.println(contest);
		isMatch = Pattern.matches(pattern, contest);
		System.out.println("匹配吗?"+isMatch);
  • 本文仅供参考,可粘贴至本地IDE测试

伍、后向引用

后向引用匹配重复单词
go go go或skt skt

  • 首先是一个单词,也就是单词开始处和结束处之间的多于一个的字母或数字(\b(\w+)\b),
  • 这个单词会被捕获到编号为1的分组中,随后是1个或几个空白符(\s+)
  • 最后为分组1中捕获的内容(也就是前面匹配的那个单词)(\1)。
System.out.println("\n=========匹配重复的单词go go go skt skt==================");
		contest = "what 452 the f***" + ".go go go skt-"+ "(010)-89153248";
		pattern = ".*\\b(\\w+)\\b\\s+\\1\\b.*";
		System.out.println(contest);
		isMatch = Pattern.matches(pattern, contest);
		System.out.println("匹配吗?"+isMatch);

六、加注释

(?#comment)