Java正则表达式中的元字符
Regex 代表正则表达式,用于定义字符串的模式。它用于查找文本或编辑文本。 Java Regex 类存在于Java.util.regex包中,在使用 regex 类的任何方法之前需要导入该包。
Java.util.regex包由 3 个类组成:
- 图案
- 匹配器
- 模式语法异常
元字符
元字符就像常见匹配模式的短代码。 Regular Expression Description \d Any digits, short-code for [0-9] \D Any non-digits, short-code for [^0-9] \s Any white space character, short-code for [\t\n\x0B\f\r] \S Any non-whitespace character \w Any word character, short-code for [a-zA-Z_0-9] \W Any non-word character \b Represents a word boundary \B Represents a non-word boundary
元字符的使用
- 在元字符前加上反斜杠 (\)。
元字符的解释
1. 数字和非数字相关元字符:(\d, \D)
Java
// Java program to demonstrate the
// Digit & Non Digit related Metacharacters
import java.io.*;
import java.util.regex.*;
class GFG {
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
}
}
Java
// Java program to demonstrate the
// Whitespace and Non-Whitespace Metacharacters
import java.io.*;
import java.util.regex.*;
class GFG {
public static void main(String[] args)
{
// comparing any whitespace character with a white
// space so return true 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
}
}
Java
// Java program to demonstrate the
// Word & Non Word Metacharacters
import java.io.*;
import java.util.regex.*;
class GFG {
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 & 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
}
}
Java
// Java program to demonstrate the
// Word & Non Word Boundary Metacharacters
import java.io.*;
import java.util.regex.*;
class GFG {
public static void main(String[] args)
{
// \b says that a string must have boundary letters
// of word characters
System.out.println(
Pattern.matches("\\bGFG\\b", "GFG")); // true
System.out.println(
Pattern.matches("\\b@GFG\\b", "@GFG")); // false
// \B says that a string must have non word
// characters as boundaries
System.out.println(Pattern.matches(
"\\B@GFG@\\B", "@GFG@")); // true
System.out.println(
Pattern.matches("\\BGFG\\B", "GFG")); // false
}
}
Java
// Java program to demonstrate all the
// Metacharacters in the Java Regex
import java.io.*;
import java.util.regex.*;
class GFG {
public static void main(String[] args)
{
// \d-number
// \D-Non Digit
// \s-Any White Space
// \S-Non White Space 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", "Geeks!")); // false
}
}
输出
true
false
true
false
解释
- d元字符表示从 0 到 9 的一个数字。因此,当我们在该范围内比较“ d ”时,它会返回true 。否则返回假。
- D元字符表示接受除 numbers 之外的任何内容的非数字。因此,当我们将“ D ”与任何数字进行比较时,它会返回 false 。否则为真。
2. 空白和非空白元字符:(\s, \S)
Java
// Java program to demonstrate the
// Whitespace and Non-Whitespace Metacharacters
import java.io.*;
import java.util.regex.*;
class GFG {
public static void main(String[] args)
{
// comparing any whitespace character with a white
// space so return true 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 。否则为假。
- S代表一个非空白字符 接受一切,除了 whitespace,因此当我们将“ S ”与空白字符进行比较时,它会返回 false 。否则为真
3. 单词和非单词元字符:(\w, \W)
Java
// Java program to demonstrate the
// Word & Non Word Metacharacters
import java.io.*;
import java.util.regex.*;
class GFG {
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 & 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 。否则为假。
- W表示一个非单词字符,它接受除字母和数字之外的任何内容。因此,当我们将“ W ”与字母或数字进行比较时,返回 false 。否则为真。
4. 单词和非单词边界元字符:(\b, \B)
Java
// Java program to demonstrate the
// Word & Non Word Boundary Metacharacters
import java.io.*;
import java.util.regex.*;
class GFG {
public static void main(String[] args)
{
// \b says that a string must have boundary letters
// of word characters
System.out.println(
Pattern.matches("\\bGFG\\b", "GFG")); // true
System.out.println(
Pattern.matches("\\b@GFG\\b", "@GFG")); // false
// \B says that a string must have non word
// characters as boundaries
System.out.println(Pattern.matches(
"\\B@GFG@\\B", "@GFG@")); // true
System.out.println(
Pattern.matches("\\BGFG\\B", "GFG")); // false
}
}
输出
true
false
true
false
解释:
- b表示字符串必须具有单词字符的边界元素,即数字或字母。所以在这里, GFG字符串有边界G, G ,它们是单词字符,所以返回true 。对于@GFG字符串,边界元素是@, G ,其中@ 不是单词字符,所以返回false 。
- B表示一个字符串必须有Non-word 字符的边界元素,即.,它可以有除数字或字母之外的任何东西。所以这里@GFG@字符串有边界 @,@ 是非单词字符,所以返回true 。对于GFG字符串,边界元素是 G, G ,它们是单词字符,返回false 。
例子:
Java
// Java program to demonstrate all the
// Metacharacters in the Java Regex
import java.io.*;
import java.util.regex.*;
class GFG {
public static void main(String[] args)
{
// \d-number
// \D-Non Digit
// \s-Any White Space
// \S-Non White Space 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", "Geeks!")); // false
}
}
输出
true
false