Java正則表示式中的元字元
正則表示式 (Regex) 指的是正則表示式。可以使用正則表示式作為 API 來定義模式以搜尋或修改文字。它經常用於指定字串約束,包括密碼和電子郵件驗證。瞭解此術語後,您可以使用正則表示式來評估您的正則表示式。
Java 正則表示式在 java.util.regex 包中提供了三個類和一個介面,分別是 MatchResult 介面、Matcher 類、Pattern 類和 PatternSyntaxException 類。Java 正則表示式功能是透過 matcher 和 pattern 類提供的。
Java正則表示式中的元字元
Java正則表示式中的元字元作為常用匹配模式的簡寫程式碼。元字元前面帶有反斜槓 (\)。下面列出了所有元字元
正則表示式 |
描述 |
|---|---|
. |
任何字元(可能匹配也可能不匹配終止符) |
\d |
任何數字,[0-9] 的簡寫 |
\D |
任何非數字,[^0-9] 的簡寫 |
\s |
任何空白字元,[\t\n\x0B\f\r] 的簡寫 |
\S |
任何非空白字元,[^\s] 的簡寫 |
\w |
任何單詞字元,[a-zA-Z_0-9] 的簡寫 |
\W |
任何非單詞字元,[^\w] 的簡寫 |
\b |
單詞邊界 |
\B |
非單詞邊界 |
元字元示例
// Java program to demonstrate all the meta characters in Java Regex
import java.io.*;
import java.util.regex.*;
public class example {
public static void main(String[] args) {
// \d- Number
// \D- Non-Digit
// \s- Any whitespace
// \S- Non whitespace Character
// \w- Any word character like numbers/ characters
// \W- Special symbols
System.out.println(Pattern.matches(
"\d\D\s\S\w\W", "1G FG!")); // true
System.out.println(Pattern.matches(
"\d\D\s\S\w\W", "Hello")); // false
}
}
輸出
true false
元字元解釋
數字和非數字元字元 (\d, \D)
示例
// Java program to demonstrate the Digit and Non-Digit related Meta Characters
import java.io.*;
import java.util.regex.*;
public class example {
Public static void main(String[] args) {
// \d represents a digit
// represents a number so return true
System.out.println(Pattern.matches("\d", "2")); // true
// Comparing a number with character so return false
System.out.println(Pattern.matches("\D", "a")); // false
// \D represents non digits
// Comparing a non-digit with character so return
// true
System.out.println(Pattern.matches("\D", "a")); // true
// Comparing a non-digit with a digit so return
// false
System.out.println(Pattern.matches("\D", "2")); // false
}
}
輸出
true false true false
解釋
d 元字元表示 0 到 9 的數字。因此,當我們在範圍內比較 "d" 時,它返回 true。否則返回 false。
D 元字元表示非數字,它接受除數字之外的任何字元。因此,當我們將 "D" 與任何數字進行比較時,它返回 false。否則返回 true。
空白和非空白元字元 (\s, \S)
示例
// Java program to demonstrate the Whitespace and Non whitespace Meta Characters
import java.io.*;
import java.util.regex.*;
Class example {
public static void main(String[] args) {
// comparing any whitespace character with a white
// space so return else false
System.out.println(Pattern.matches("\s", " ")); // true
System.out.println(Pattern.matches("\s", "2")); // false
// comparing any non-whitespace character with a non
// white space character so return true else false
System.out.println(Pattern.matches("\S", "2")); // true
System.out.println(Pattern.matches("\S", " ")); // false
}
}
輸出
true false true false
解釋
s 代表空格字元,例如空格、製表符、換行符等。因此,當我們將 "s" 與空格字元進行比較時,它將返回 true。否則返回 false
S 代表非空白字元,它接受除空白字元之外的所有字元,因此當我們將 "S" 與空格字元進行比較時,它將返回 false。否則返回 true。
單詞和非單詞元字元 (\w, \W)
示例
// Java program to demonstrate the Word and Non-Word Meta Characters
import java.io.*;
import java.util.regex.*;
public class example {
public static void main(String[] args){
// comparing any word character with a word
// character so return true else false
System.out.println(Pattern.matches("\w", "a")); // true
System.out.println(Pattern.matches("\w", "2")); // true
System.out.println(Pattern.matches("\w", "$")); // false
// comparing any non-word character with special
// symbols and whitespaces return true else false
System.out.println(Pattern.matches("\W", "2")); // false
System.out.println(Pattern.matches("\W", " ")); // true
System.out.println(Pattern.matches("\W", "$")); // true
}
}
輸出
true true false false true true
解釋
w 代表單詞字元,它接受字母(大寫和小寫)和數字 [0-9]。因此,當我們將 "w" 與字母或數字進行比較時,它返回 true。否則返回 false。
W 代表非單詞字元,它接受除字母和數字之外的任何字元。因此,當我們將 "W" 與字母或數字進行比較時,它返回 false。否則返回 true。
單詞和非單詞元字元 (\b, \B)
示例
// Java program to demonstrate the Word and Non Word boundary Meta Characters
import java.io.*;
import java.util.regex.*;
class example {
public static void main(String[] args) {
// \b says that a string must have boundary letters
// of word characters
System.out.println(Pattern.matches("\bexample\b", "example")); // true
System.out.println(Pattern.matches("\b@example\b", "@example")); // false
// \B says that a string must have non- word characters as boundaries
System.out.println(Pattern.matches("\B@example@\B", "@example@")); //true
System.out.println(Pattern.matches("\Bexample\B", "example")); //false
}
}
輸出
true false true false
解釋
根據符號 b,字串必須具有單詞字元(例如數字或字母)的邊界元素。由於在這種情況下,GFG 字串包含作為單詞字元的邊界 (G, G),因此它返回 true。@GFG 字串的邊界元素是 @、G,其中 @ 不是單詞字元,因此返回 false。
B 表示字串的邊界元素必須是非單詞字元;允許任何非數字或字母的內容。由於 @GFG@ 字串的邊界是 @、@ 非單詞字元,因此它返回 true。GFG 字串的邊界元素是 G 和 G,它們是單詞字元,因此返回 false。
結論
對於尋求檢測或操作文字資訊的程式設計師來說,正則表示式(通常稱為正則表示式)提供了一種有效的解決方案。透過在字串中定義模式,開發人員可以輕鬆找到所需內容,甚至可以修改其外觀。Java 正則表示式類位於 java.util.regex 包中。在使用任何 Java 正則表示式方法之前,需要匯入它。Java 正則表示式包含三個類和一個介面,分別是 pattern、matcher、PatternSyntaxException 和 MatchResult 介面。
資料結構
網路
關係型資料庫管理系統 (RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP