最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

java中String.matches方法使用

 更新時間:2024年09月20日 09:51:31   作者:似霰  
String.matches()方法用于檢測字符串是否符合特定的正則表達式,詳細介紹了如何使用String.matches()配合不同的正則表達式來匹配各種特定格式的字符串,感興趣的可以了解一下

一、String.matches()方法簡介

1.1 概述

matches() 方法用于檢測字符串是否匹配給定的正則表達式。

調(diào)用此方法的 str.matches(regex) 形式與以下表達式產(chǎn)生的結(jié)果完全相同:

Pattern.matches(regex, str)

1.2 語法、參數(shù)、返回值

語法
public boolean matches(String regex)
參數(shù)
regex -- 匹配字符串的正則表達式。
返回值
在字符串匹配給定的正則表達式時,返回 true。

二、String.matches()配合正則表達式使用

2.1 普通字符匹配

字母、數(shù)字、漢字、下劃線、以及沒有特殊定義的標點符號,都是 “普通字符”。表達式中的普通字符,在匹配一個字符串的時候,匹配與之相同的一個字符。

字符描述
[abc]匹配[…]中所有的字符
[^abc]匹配除了[…]中所有的字符
[A-Z]匹配所有區(qū)間A-Z的字符
[0-9]表示區(qū)間,匹配0-9的數(shù)字
.匹配除了換行符(\r、\n)之外的任何單個字符,相當于[^\r\n]

2.1.1 [abc]

public static void main(String[] args) {
		String s1 = "a";
		boolean result = s1.matches("[abc]");
		System.out.println(s1 + " 匹配[abc]結(jié)果   " + result);
		s1 = "b";
		result = s1.matches("[abc]");
		System.out.println(s1 + " 匹配[abc]結(jié)果   " + result);
		s1 = "c";
		result = s1.matches("[abc]");
		System.out.println(s1 + " 匹配[abc]結(jié)果   " + result);
		s1 = "ab";
		result = s1.matches("[abc]");
		System.out.println(s1 + " 匹配[abc]結(jié)果   " + result);
		s1 = "d";
		result = s1.matches("[abc]");
		System.out.println(s1 + " 匹配[abc]結(jié)果   " + result);
	}

Result:

在這里插入圖片描述

解析:
這個正則表達式表示匹配包含單個字符的字符串,該字符可以是 a、b 或 c 中的任意一個。換句話說,它會檢查字符串是否只包含 a、b 或 c 中的任意一個字符。
例如:
“a” 會匹配成功,因為它只包含一個字符并且是 a。
“b” 也會匹配成功,因為它只包含一個字符并且是 b。
“c” 同樣會匹配成功,因為它只包含一個字符并且是 c。
“ab” 不會匹配成功,因為它包含了多個字符。
“d” 也不會匹配成功,因為它不是 a、b 或 c 中的任意一個字符。
因此,如果字符串僅包含 a、b 或 c 中的任意一個字符,String.matches(“[abc]”) 方法將返回 true,如果字符串包含其他字符或者包含多個字符,則返回 false。

2.1.2 [^abc]

	public static void main(String[] args) {
		String s1 = "a";
		boolean result = s1.matches("[^abc]");
		System.out.println(s1 + " 匹配[^abc]結(jié)果   " + result);
		s1 = "b";
		result = s1.matches("[^abc]");
		System.out.println(s1 + " 匹配[^abc]結(jié)果   " + result);
		s1 = "c";
		result = s1.matches("[^abc]");
		System.out.println(s1 + " 匹配[^abc]結(jié)果   " + result);
		s1 = "d";
		result = s1.matches("[^abc]");
		System.out.println(s1 + " 匹配[^abc]結(jié)果   " + result);
		s1 = "e";
		result = s1.matches("[^abc]");
		System.out.println(s1 + " 匹配[^abc]結(jié)果   " + result);
		s1 = "ab";
		result = s1.matches("[^abc]");
		System.out.println(s1 + " 匹配[^abc]結(jié)果   " + result);
	}

Result:

在這里插入圖片描述

解析:
在正則表達式中,^ 表示取反,[^abc] 表示匹配任意一個字符,但是不能是 a、b 或 c 中的任何一個。
例如:
“a” 也不會匹配成功,因為它是 a。
“b” 也不會匹配成功,因為它是 b。
“c” 也不會匹配成功,因為它是 c。
“d” 會匹配成功,因為它只包含一個字符并且不是 a、b 或 c。
“e” 也會匹配成功,因為它只包含一個字符并且不是 a、b 或 c。
“ab” 不會匹配成功,因為它包含了多個字符。
因此,如果字符串只包含一個字符并且不是 a、b 或 c 中的任何一個,String.matches(“[^abc]”) 方法將返回 true,如果字符串包含其他字符或者包含多個字符,則返回 false。

2.1.3 [A-Z]

public static void main(String[] args) {
		String s1 = "A";
		boolean result = s1.matches("[A-Z]");
		System.out.println(s1 + " 匹配[A-Z]結(jié)果   " + result);
		s1 = "B";
		result = s1.matches("[A-Z]");
		System.out.println(s1 + " 匹配[A-Z]結(jié)果   " + result);
		s1 = "Z";
		result = s1.matches("[A-Z]");
		System.out.println(s1 + " 匹配[A-Z]結(jié)果   " + result);
		s1 = "a";
		result = s1.matches("[A-Z]");
		System.out.println(s1 + " 匹配[A-Z]結(jié)果   " + result);
		s1 = "AB";
		result = s1.matches("[A-Z]");
		System.out.println(s1 + " 匹配[A-Z]結(jié)果   " + result);
		s1 = "1";
		result = s1.matches("[A-Z]");
		System.out.println(s1 + " 匹配[A-Z]結(jié)果   " + result);

	}

Result:

在這里插入圖片描述

解析:
這個正則表達式表示匹配一個大寫字母,即 A 到 Z 中的任意一個字母。
例如:
“A” 會匹配成功,因為它是大寫字母 A。
“B” 也會匹配成功,因為它是大寫字母 B。
“Z” 也會匹配成功,因為它是大寫字母 Z。
“a” 不會匹配成功,因為它是小寫字母。
“AB” 不會匹配成功,因為它包含多個字符。
“1” 也不會匹配成功,因為它不是字母。
因此,如果字符串只包含一個大寫字母,String.matches(“[A-Z]”) 方法將返回 true,如果字符串包含其他字符、小寫字母或者包含多個字符,返回 false。

2.1.4 [0-9]

public static void main(String[] args) {
		String s1 = "0";
		boolean result = s1.matches("[0-9]");
		System.out.println(s1 + " 匹配[0-9]結(jié)果   " + result);
		s1 = "5";
		result = s1.matches("[0-9]");
		System.out.println(s1 + " 匹配[0-9]結(jié)果   " + result);
		s1 = "9";
		result = s1.matches("[0-9]");
		System.out.println(s1 + " 匹配[0-9]結(jié)果   " + result);
		s1 = "a";
		result = s1.matches("[0-9]");
		System.out.println(s1 + " 匹配[0-9]結(jié)果   " + result);
		s1 = "12";
		result = s1.matches("[0-9]");
		System.out.println(s1 + " 匹配[0-9]結(jié)果   " + result);
		s1 = "x";
		result = s1.matches("[0-9]");
		System.out.println(s1 + " 匹配[0-9]結(jié)果   " + result);

	}

Result:

在這里插入圖片描述

解析:
這個正則表達式表示匹配一個數(shù)字字符,即 0 到 9 中的任意一個數(shù)字。
例如:
“0” 會匹配成功,因為它是數(shù)字 0。
“5” 也會匹配成功,因為它是數(shù)字 5。
“9” 也會匹配成功,因為它是數(shù)字 9。
“a” 不會匹配成功,因為它不是數(shù)字。
“12” 不會匹配成功,因為它包含多個字符。
“x” 也不會匹配成功,因為它不是數(shù)字。
因此,如果字符串只包含一個數(shù)字字符,String.matches(“[0-9]”) 方法將返回 true,如果字符串包含其他字符或者包含多個字符,返回 false。

2.1.5 .

		public static void main(String[] args) {
		String s1 = "a";
		boolean result = s1.matches(".");
		System.out.println(s1 + " 匹配.結(jié)果   " + result);
		s1 = "1";
		result = s1.matches(".");
		System.out.println(s1 + " 匹配.結(jié)果   " + result);
		s1 = " ";
		result = s1.matches(".");
		System.out.println(s1 + " 匹配.結(jié)果   " + result);
		s1 = "\n";
		result = s1.matches(".");
		System.out.println(s1 + " 匹配.結(jié)果   " + result);
		s1 = "\r";
		result = s1.matches(".");
		System.out.println(s1 + " 匹配.結(jié)果   " + result);
		s1 = "ab";
		result = s1.matches(".");
		System.out.println(s1 + " 匹配.結(jié)果   " + result);
		System.out.println(" 斜杠r的作用 \r 斜杠n的作用 \n 都是換行 " );
	}

Result:

在這里插入圖片描述

解析:
這個正則表達式表示匹配任意一個字符,除了換行符以外的所有字符。
例如:
“a” 會匹配成功,因為它只包含一個字符。
“1” 也會匹配成功,因為它只包含一個字符。
" " 也會匹配成功,因為它只包含一個空格字符。
“\n” 不會匹配成功,因為它是一個換行符。
“\r” 不會匹配成功,因為它是一個換行符。
“ab” 不會匹配成功,因為它包含了多個字符。
因此,如果字符串只包含一個字符,String.matches(“.”) 方法將返回 true,如果字符串包含多個字符或者包含換行符,返回 false。

2.2 單個符號匹配

字符描述
[ ]只有方括號里面指定的字符才參與匹配,也只能匹配單個字符。
|相當于“或”,可以匹配指定的字符,但是也只能選擇其中一項進行匹配。
^表示否,如果用在方括號內(nèi),^表示不想匹配的字符。
\S非空字符
\s空字符,只可以匹配一個空格、制表符、回車符、換頁符,不可以匹配自己輸入的多個空格。

2.2.1 中括號[ ]

public static void main(String[] args) {
		String s1 = "a";
		boolean result = s1.matches("a[abc]d");
		System.out.println(s1 + " 匹配a[abc]d  結(jié)果   " + result);
		s1 = "ad";
		result = s1.matches("a[abc]d");
		System.out.println(s1 + " 匹配a[abc]d  結(jié)果   " + result);
		s1 = "aad";
		result = s1.matches("a[abc]d");
		System.out.println(s1 + " 匹配a[abc]d  結(jié)果   " + result);
		s1 = "abd";
		result = s1.matches("a[abc]d");
		System.out.println(s1 + " 匹配a[abc]d  結(jié)果   " + result);
		s1 = "acd";
		result = s1.matches("a[abc]d");
		System.out.println(s1 + " 匹配a[abc]d  結(jié)果   " + result);
		s1 = "aod";
		result = s1.matches("a[abc]d");
		System.out.println(s1 + " 匹配a[abc]d  結(jié)果   " + result);
		s1 = "abcd";
		result = s1.matches("a[abc]d");
		System.out.println(s1 + " 匹配a[abc]d  結(jié)果   " + result);
	}

Result:

在這里插入圖片描述

解析:
這個正則表達式表示匹配一個以"a"開頭,然后是"a"、“b” 或 “c” 中的任意一個字符,然后是"d"結(jié)尾的字符串。
例如:
“a” 不會匹配成功,因為它的第二個字符不是"a"、“b” 或 “c” 中的任意一個,且沒有以d結(jié)尾。
“ad” 不會匹配成功,因為它的第二個字符不是"a"、“b” 或 “c” 中的任意一個。
“aad” 會匹配成功,因為它以"a"開頭,然后是"a",最后是"d"。
“abd” 會匹配成功,因為它以"a"開頭,然后是"b",最后是"d"。
“acd” 會匹配成功,因為它以"a"開頭,然后是"c",最后是"d"。
“aod” 不會匹配成功,因為它的第二個字符不是"a"、“b” 或 “c” 中的任意一個。
“abcd” 不會匹配成功,因為它包含了多個字符。
因此,如果字符串符合以"a"開頭,然后是"a"、“b” 或 “c” 中的任意一個字符,最后是"d"結(jié)尾的模式,String.matches(“a[abc]d”) 方法將返回 true,如果字符串不符合這個模式,返回 false。

2.2.2 或符號 |

public static void main(String[] args) {
		String s1 = "a";
		boolean result = s1.matches("a(a|b|c)d");
		System.out.println(s1 + " 匹配a(a|b|c)d  結(jié)果   " + result);
		s1 = "ad";
		result = s1.matches("a(a|b|c)d");
		System.out.println(s1 + " 匹配a(a|b|c)d  結(jié)果   " + result);
		s1 = "aad";
		result = s1.matches("a(a|b|c)d");
		System.out.println(s1 + " 匹配a(a|b|c)d  結(jié)果   " + result);
		s1 = "abd";
		result = s1.matches("a(a|b|c)d");
		System.out.println(s1 + " 匹配a(a|b|c)d  結(jié)果   " + result);
		s1 = "acd";
		result = s1.matches("a(a|b|c)d");
		System.out.println(s1 + " 匹配a(a|b|c)d  結(jié)果   " + result);
		s1 = "aod";
		result = s1.matches("a(a|b|c)d");
		System.out.println(s1 + " 匹配a(a|b|c)d  結(jié)果   " + result);
		s1 = "abcd";
		result = s1.matches("a(a|b|c)d");
		System.out.println(s1 + " 匹配a(a|b|c)d  結(jié)果   " + result);
	}

Result:

在這里插入圖片描述

解析:
這個正則表達式表示匹配一個以"a"開頭,然后是"a"、“b” 或 “c” 中的任意一個字符,最后是"d"結(jié)尾的字符串。在正則表達式中,(a|b|c) 表示一個分組,其中的字符"a"、“b” 或 “c"中的任意一個字符。
例如:
“a” 不會匹配成功,因為它的第二個字符不是"a”、“b” 或 “c” 中的任意一個,且沒有以d結(jié)尾。
“ad " 不會匹配成功,因為它的第二個字符不是"a”、“b” 或 “c” 中的任意一個。
“aad” 會匹配成功,因為它以"a “開頭,然后是"a”,最后是"d"。
“abd” 會匹配成功,因為它以"a"開頭,然后是"b",最后是"d"。
“acd” 會匹配成功,因為它以"a"開頭,然后是"c",最后是"d"。
“aod” 不會匹配成功,因為它的第二個字符不是"a"、“b” 或 “c” 中的任意一個。
“abcd” 不會匹配成功,因為它包含了多個字符。
因此,如果字符串符合以"a"開頭,然后是"a"、“b” 或 “c” 中的任意一個字符,最后是"d"結(jié)尾的模式,String.matches(“a(a|b|c)d”) 方法將返回 true,如果字符串不符合這個模式,返回 false。

2.2.3 ^ 符號

	public static void main(String[] args) {
		String s1 = "a";
		boolean result = s1.matches("[^abc]");
		System.out.println(s1 + " 匹配[^abc]  結(jié)果   " + result);
		s1 = "d";
		result = s1.matches("[^abc]");
		System.out.println(s1 + " 匹配[^abc]  結(jié)果   " + result);
		s1 = "e";
		result = s1.matches("[^abc]");
		System.out.println(s1 + " 匹配[^abc]  結(jié)果   " + result);
		s1 = "ab";
		result = s1.matches("[^abc]");
		System.out.println(s1 + " 匹配[^abc]  結(jié)果   " + result);
	}

Result:

在這里插入圖片描述

解析:
這個正則表達式表示匹配一個不是"a"、“b” 或 “c” 中的任意一個字符的字符串。方括號內(nèi)的^ 表示取反,表示匹配不在指定字符集合中的任意一個字符。
例如:
“a” 不會匹配成功,因為它是"a"、“b” 或 “c” 中的一個字符。
“d” 會匹配成功,因為它不是"a"、“b” 或 “c” 中的任意一個字符。
“e” 會匹配成功,因為它不是"a"、“b” 或 “c” 中的任意一個字符。
“ab” 不會匹配成功,因為它包含了"a"和"b"兩個字符。
因此,如果字符串不是"a"、“b” 或 “c” 中的任意一個字符,String.matches(“[^abc]”) 方法將返回 true,如果字符串是這些字符之一,返回 false。

2.2.4 \S 符號

	public static void main(String[] args) {
		String s1 = "a";
		boolean result = s1.matches("\\S");
		System.out.println(s1 + " 匹配\\S  結(jié)果   " + result);
		s1 = "1";
		result = s1.matches("\\S");
		System.out.println(s1 + " 匹配\\S  結(jié)果   " + result);
		s1 = "!";
		result = s1.matches("\\S");
		System.out.println(s1 + " 匹配\\S  結(jié)果   " + result);
		s1 = " ";
		result = s1.matches("\\S");
		System.out.println(s1 + " 匹配\\S  結(jié)果   " + result);
		s1 = "\t";
		result = s1.matches("\\S");
		System.out.println(s1 + " 匹配\\S  結(jié)果   " + result);
	}

Result:

在這里插入圖片描述

解析:
在正則表達式中,\S 表示匹配任意非空白字符。這包括字母、數(shù)字、標點符號等,只要不是空格、制表符等空白字符就可以匹配。
例如:
“a” 會匹配成功,因為它是一個非空白字符。
“1” 會匹配成功,因為它是一個非空白字符。
“!” 會匹配成功,因為它是一個非空白字符。
" " 不會匹配成功,因為它是一個空白字符。
“\t” 不會匹配成功,因為它是一個空白字符。
因此,如果字符串是一個非空白字符,String.matches(“\S”) 方法將返回 true,如果字符串是一個空白字符,返回 false。

2.2.5 \s 符號

	public static void main(String[] args) {
		String s1 = " ";
		boolean result = s1.matches("\\s");
		System.out.println(s1 + " 匹配\\s  結(jié)果   " + result);
		s1 = "\t";
		result = s1.matches("\\s");
		System.out.println(s1 + " 匹配\\s  結(jié)果   " + result);
		s1 = "\n";
		result = s1.matches("\\s");
		System.out.println(s1 + " 匹配\\s  結(jié)果   " + result);
		s1 = "a";
		result = s1.matches("\\s");
		System.out.println(s1 + " 匹配\\s  結(jié)果   " + result);
	}

Result:

在這里插入圖片描述

解析:
在正則表達式中,\s 表示匹配任意空白字符。這包括空格、制表符、換行符等。
例如:
" " 會匹配成功,因為它是一個空白字符。
“\t” 會匹配成功,因為它是一個空白字符。
“\n” 會匹配成功,因為它是一個空白字符。
“a” 不會匹配成功,因為它不是一個空白字符。
因此,如果字符串是一個空白字符,String.matches(“\s”) 方法將返回 true,如果字符串不是一個空白字符,返回 false。

2.3 轉(zhuǎn)義字符

字符描述
\^匹配^符號本身
\$匹配$符號本身
\.匹配小數(shù)點本身
\r,\n回車和換行符
\t制表符
\\代表 ”\“ 本身

這個就比較簡單不做代碼測試了,需要注意的是對于Java中的\\理解

Java中正則表達式的\
\表示將下一字符標記為特殊字符。如\d表示數(shù)字字符匹配,等效于 [0-9]。\w表示匹配任何字類字符(字母數(shù)字下劃線),注意包括下劃線。與"[A-Za-z0-9_]"等效。
\\中的第一個\表示java的轉(zhuǎn)義字符\ 由編譯器解析,第二個\是正則表達式\ 由正則表達式引擎解析。

關(guān)于Java正則和轉(zhuǎn)義中\(zhòng)和\和\\的理解

2.4 匹配 ‘多種字符’ 其中的任意一個字符

字符描述
\d任意一個數(shù)字 0-9中任意一個
\w任意一個字母或數(shù)字或下劃線,也就是A-Z,a-z,0-9,_,任意一個
\s空格、制表符、換行符等空白字符中的任意一個

\s上面已經(jīng)有測試例子了,看一下\d 和\w的簡單測試

2.4.1 \d

\d ,但在java中需要\\d

	public static void main(String[] args) {
		String s1 = "1";
		boolean result = s1.matches("\\d");
		System.out.println(s1 + " 匹配\\d  結(jié)果   " + result);
		s1 = "5";
		result = s1.matches("\\d");
		System.out.println(s1 + " 匹配\\d  結(jié)果   " + result);
		s1 = "a";
		result = s1.matches("\\d");
		System.out.println(s1 + " 匹配\\d  結(jié)果   " + result);
		s1 = "10";
		result = s1.matches("\\d");
		System.out.println(s1 + " 匹配\\d  結(jié)果   " + result);
	}

Result:

在這里插入圖片描述

解析:
在正則表達式中,\d 表示匹配一個數(shù)字字符。換句話說,它只匹配數(shù)字 0-9 中的任意一個字符。
例如:
“1” 會匹配成功,因為它是一個數(shù)字字符。
“5” 會匹配成功,因為它是一個數(shù)字字符。
“a” 不會匹配成功,因為它不是一個數(shù)字字符。
“10” 不會匹配成功,因為它包含了兩個字符。
因此,如果字符串是一個數(shù)字字符,String.matches(“\\d”) 方法將返回 true,如果字符串不是一個數(shù)字字符,并且返回 false。

2.4.2 \w

	public static void main(String[] args) {
		String s1 = "a";
		boolean result = s1.matches("\\w");
		System.out.println(s1 + " 匹配\\w  結(jié)果   " + result);
		s1 = "5";
		result = s1.matches("\\w");
		System.out.println(s1 + " 匹配\\w  結(jié)果   " + result);
		s1 = "_";
		result = s1.matches("\\w");
		System.out.println(s1 + " 匹配\\w  結(jié)果   " + result);
		s1 = "!";
		result = s1.matches("\\w");
		System.out.println(s1 + " 匹配\\w  結(jié)果   " + result);
	}

Result:

在這里插入圖片描述

解析:
在正則表達式中,\w 表示匹配一個單詞字符,包括字母、數(shù)字和下劃線。換句話說,它匹配字母數(shù)字字符以及下劃線。
例如:
“a” 會匹配成功,因為它是一個單詞字符。
“5” 會匹配成功,因為它是一個單詞字符。
“_” 會匹配成功,因為它是一個單詞字符。
“!” 不會匹配成功,因為它不是一個單詞字符。
因此,如果字符串是一個單詞字符,String.matches(“\\w”) 方法將返回 true,如果字符串不是一個單詞字符,返回 false。

2.5 匹配次數(shù)的特殊符號

前面的表達式,無論是單個字符,還是多種字符其中的一個,都只能匹配一次。如果使用表達式再加上修飾匹配次數(shù)的特殊符號,那么不用重復書寫表達式就可以重復多次匹配。
注:特殊符號修飾的是它前面的表達式

字符描述
{n}表達式重復n次,例”\d{2}“相當于”\d\d“,可以匹配到11,12, 13
{m,n}表達式至少重復m次,最多重復n次,例”\w{2,3}",可以匹配ac,ad,acd,但是無法匹配abcd
{m,}表達式至少重復m次,最多不設(shè)限
?表達式0次或1次,相當于{0,1}
+表達式至少出現(xiàn)1次,相當于{1,}
*表達式不出現(xiàn)或出現(xiàn)任意次,相當于{0,}

做一個測試例子:

public static void main(String[] args) {
		System.out.println("------{n}測試-------");
		String s1 = "1";
		boolean result = s1.matches("\\d{2}");
		System.out.println(s1 + " 匹配\\d{2}  結(jié)果   " + result);
		s1 = "12";
		result = s1.matches("\\d{2}");
		System.out.println(s1 + " 匹配\\d{2}  結(jié)果   " + result);
		s1 = "123";
		result = s1.matches("\\d{2}");
		System.out.println(s1 + " 匹配\\d{2}  結(jié)果   " + result);
		System.out.println("------{n}測試完畢-------"+"\n");
		
		System.out.println("------{m,n}測試-------");
		s1 = "1";
	    result = s1.matches("\\w{2,3}");
		System.out.println(s1 + " 匹配\\w{2,3}  結(jié)果   " + result);
		s1 = "12";
		result = s1.matches("\\w{2,3}");
		System.out.println(s1 + " 匹配\\w{2,3}  結(jié)果   " + result);
		s1 = "123";
		result = s1.matches("\\w{2,3}");
		System.out.println(s1 + " 匹配\\w{2,3}  結(jié)果   " + result);
		s1 = "1234";
		result = s1.matches("\\w{2,3}");
		System.out.println(s1 + " 匹配\\w{2,3}  結(jié)果   " + result);
		System.out.println("------{m,n}測試完畢-------"+"\n");
		
		System.out.println("------{m,}測試-------");
		s1 = "1";
	    result = s1.matches("\\w{3,}");
		System.out.println(s1 + " 匹配\\w{3,}  結(jié)果   " + result);
		s1 = "12";
		result = s1.matches("\\w{3,}");
		System.out.println(s1 + " 匹配\\w{3,}  結(jié)果   " + result);
		s1 = "123";
		result = s1.matches("\\w{3,}");
		System.out.println(s1 + " 匹配\\w{3,}  結(jié)果   " + result);
		s1 = "1234";
		result = s1.matches("\\w{3,}");
		System.out.println(s1 + " 匹配\\w{3,}  結(jié)果   " + result);
		System.out.println("------{m,}測試完畢-------"+"\n");
		
		
		System.out.println("------?測試-------");
		s1 = "ab";
	    result = s1.matches("ab[cd]?e");
		System.out.println(s1 + " 匹配ab[cd]?e  結(jié)果   " + result);
		s1 = "abe";
		result = s1.matches("ab[cd]?e");
		System.out.println(s1 + " 匹配ab[cd]?e 結(jié)果   " + result);
		s1 = "abce";
		result = s1.matches("ab[cd]?e");
		System.out.println(s1 + " 匹配ab[cd]?e  結(jié)果   " + result);
		s1 = "abcde";
		result = s1.matches("ab[cd]?e");
		System.out.println(s1 + " 匹配ab[cd]?e  結(jié)果   " + result);
		System.out.println("------?測試完畢-------"+"\n");
		
		System.out.println("------+測試-------");
		s1 = "ab";
	    result = s1.matches("ab[cd]+e");
		System.out.println(s1 + " 匹配ab[cd]+e  結(jié)果   " + result);
		s1 = "abe";
		result = s1.matches("ab[cd]+e");
		System.out.println(s1 + " 匹配ab[cd]+e 結(jié)果   " + result);
		s1 = "abce";
		result = s1.matches("ab[cd]+e");
		System.out.println(s1 + " 匹配ab[cd]+e  結(jié)果   " + result);
		s1 = "abcde";
		result = s1.matches("ab[cd]+e");
		System.out.println(s1 + " 匹配ab[cd]+e  結(jié)果   " + result);
		System.out.println("------+測試完畢-------"+"\n");
		
		System.out.println("------*測試-------");
		s1 = "ab";
	    result = s1.matches("ab[cd]*e");
		System.out.println(s1 + " 匹配ab[cd]*e  結(jié)果   " + result);
		s1 = "abe";
		result = s1.matches("ab[cd]*e");
		System.out.println(s1 + " 匹配ab[cd]*e 結(jié)果   " + result);
		s1 = "abce";
		result = s1.matches("ab[cd]*e");
		System.out.println(s1 + " 匹配ab[cd]*e  結(jié)果   " + result);
		s1 = "abcde";
		result = s1.matches("ab[cd]*e");
		System.out.println(s1 + " 匹配ab[cd]*e  結(jié)果   " + result);
		System.out.println("------*測試完畢-------"+"\n");
	}

Result:

------{n}測試-------
1 匹配\d{2}  結(jié)果   false
12 匹配\d{2}  結(jié)果   true
123 匹配\d{2}  結(jié)果   false
------{n}測試完畢-------

------{m,n}測試-------
1 匹配\w{2,3}  結(jié)果   false
12 匹配\w{2,3}  結(jié)果   true
123 匹配\w{2,3}  結(jié)果   true
1234 匹配\w{2,3}  結(jié)果   false
------{m,n}測試完畢-------

------{m,}測試-------
1 匹配\w{3,}  結(jié)果   false
12 匹配\w{3,}  結(jié)果   false
123 匹配\w{3,}  結(jié)果   true
1234 匹配\w{3,}  結(jié)果   true
------{m,}測試完畢-------

------?測試-------
ab 匹配ab[cd]?e  結(jié)果   false
abe 匹配ab[cd]?e 結(jié)果   true
abce 匹配ab[cd]?e  結(jié)果   true
abcde 匹配ab[cd]?e  結(jié)果   false
------?測試完畢-------

------+測試-------
ab 匹配ab[cd]+e  結(jié)果   false
abe 匹配ab[cd]+e 結(jié)果   false
abce 匹配ab[cd]+e  結(jié)果   true
abcde 匹配ab[cd]+e  結(jié)果   true
------+測試完畢-------

------*測試-------
ab 匹配ab[cd]*e  結(jié)果   false
abe 匹配ab[cd]*e 結(jié)果   true
abce 匹配ab[cd]*e  結(jié)果   true
abcde 匹配ab[cd]*e  結(jié)果   true
------*測試完畢-------

三、其他正則表達式

表達式實在太多而且可以自定義無窮無盡,但是相信小白看完這篇文章后,對于java使用正則表達式已經(jīng)入門了,附上一些參考鏈接,里面有很多正則表達式的例子。

關(guān)于Java正則和轉(zhuǎn)義中\(zhòng)和\和\\的理解
Java 正則表達式的用法和實例
Java 之正則表達式語法及常用正則表達式匯總
java正則表達式大全(常用)

到此這篇關(guān)于java中String.matches方法使用的文章就介紹到這了,更多相關(guān)java String.matches內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家! 

相關(guān)文章

  • Java圓通物流軌跡推送服務(wù)接口文檔及流程

    Java圓通物流軌跡推送服務(wù)接口文檔及流程

    這篇文章主要介紹了圓通物流軌跡推送服務(wù)接口Java文檔,主要用來接收圓通推送的訂單狀態(tài),本文給大家分享詳細流程,感興趣的朋友跟隨小編一起看看吧
    2022-02-02
  • Mysql在Spring Boot項目中的完整配置教程

    Mysql在Spring Boot項目中的完整配置教程

    本文詳細介紹了在Spring Boot項目中配置Mysql數(shù)據(jù)庫的各個方面,包括基礎(chǔ)配置、高級配置、JPA/Hibernate配置、事務(wù)管理、性能優(yōu)化、安全配置以及故障排除,同時,提供了常用配置參數(shù)的說明和連接池的選擇建議,感興趣的朋友跟隨小編一起看看吧
    2026-02-02
  • java sftp下載文件報錯Caused by:com.jcraft.jsch.JSchException:session is down問題

    java sftp下載文件報錯Caused by:com.jcraft.jsch.JSchExcep

    文章講述了作者在日常工作中遇到的JSch連接問題,經(jīng)過分析發(fā)現(xiàn)是由于連接泄露導致的,作者提出了解決方案,并給出了使用建議:1.在finally代碼塊中關(guān)閉連接;2.在真正使用階段再創(chuàng)建連接,避免創(chuàng)建后不使用又忘記關(guān)閉連接
    2024-11-11
  • Spring?Boot分離配置文件的多種方式總結(jié)

    Spring?Boot分離配置文件的多種方式總結(jié)

    Spring Boot可以外部化程序配置,以便可以在不同環(huán)境中使用相同的應用程序代碼;當然Spring Boot可以將配置文件進行拆分,以便于激活不同的運行環(huán)境,下面這篇文章主要給大家總結(jié)介紹了關(guān)于Spring?Boot分離配置文件的多種方式,需要的朋友可以參考下
    2022-11-11
  • Mybatis-plus配置之日期時間自動填充實踐

    Mybatis-plus配置之日期時間自動填充實踐

    本文介紹如何使用MyBatis-Plus的MetaObjectHandler接口實現(xiàn)新增和更新時間的自動填充,通過繼承抽象類、添加注解及處理版本兼容性,簡化開發(fā)流程并減少手動操作
    2025-08-08
  • Spring @Async 的使用與實現(xiàn)的示例代碼

    Spring @Async 的使用與實現(xiàn)的示例代碼

    本篇文章主要介紹了Spring @Async 的使用與實現(xiàn)的示例代碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-08-08
  • IDEA2020導入非maven項目并部署tomcat的方法

    IDEA2020導入非maven項目并部署tomcat的方法

    這篇文章主要介紹了IDEA 2020 導入非maven項目并部署tomcat的方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-07-07
  • springboot 增加過濾器方法操作示例

    springboot 增加過濾器方法操作示例

    這篇文章主要介紹了springboot 增加過濾器方法操作,結(jié)合實例形式分析了springboot過濾器配置、加載等相關(guān)操作技巧,需要的朋友可以參考下
    2019-12-12
  • 解決MyBatis報錯:There is no getter for property named'Xxx'in'class xxx.xxx.Xxx'

    解決MyBatis報錯:There is no getter for 

    這篇文章主要介紹了解決MyBatis報錯:There is no getter for property named'Xxx'in'class xxx.xxx.Xxx'問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-08-08
  • Spring?IOC注入的兩種方式詳解以及代碼示例

    Spring?IOC注入的兩種方式詳解以及代碼示例

    在Spring框架中,依賴注入(Dependency?Injection,DI)是通過控制反轉(zhuǎn)(Inversion?of?Control,IOC)實現(xiàn)的,Spring提供了多種方式來實現(xiàn)IOC注入,本文就給大家介紹兩種注入的方式:基于XML和基于注解,文中有詳細的代碼示例,需要的朋友可以參考下
    2023-08-08

最新評論

县级市| 固阳县| 龙江县| 拜泉县| 邵武市| 怀柔区| 广河县| 齐齐哈尔市| 仪陇县| 普兰店市| 高陵县| 宽甸| 长泰县| 吉木乃县| 荥经县| 巴楚县| 浦城县| 鹤山市| 垣曲县| 琼中| 慈溪市| 云梦县| 吉木萨尔县| 汉寿县| 綦江县| 资源县| 焦作市| 永春县| 大城县| 东乌珠穆沁旗| 年辖:市辖区| 丁青县| 福海县| 西昌市| 武平县| 丘北县| 眉山市| 双鸭山市| 武川县| 景宁| 如东县|