YouTip LogoYouTip

Java Regex Pattern

The `Pattern` class in Java is one of the core components of regular expression functionality, belonging to the `java.util.regex` package. Regular Expression (Regex) is a powerful text processing tool used to match, find, and replace specific patterns in strings. The main purpose of the `Pattern` class is to compile a regular expression into a pattern (Pattern) for efficient subsequent matching operations. * * * ## Introduction to Regular Expressions Regular expressions are a syntax for describing string patterns. Through specific symbols and rules, complex string matching rules can be defined. For example: * `d` matches any digit (equivalent to ``) * `w` matches any letter, digit, or underscore (equivalent to ``) * `a+` matches one or more consecutive letters `a` * `^` matches the beginning of a string * `$` matches the end of a string * * * ## Basic Usage of Pattern Class ### Creating Pattern Objects The `Pattern` class has no public constructor; instances must be created through the static method `compile()`: ## Example import java.util.regex.Pattern; // Compile regular expression Pattern pattern = Pattern.compile("a*b"); ### Common Methods #### `matcher(CharSequence input)` Creates a `Matcher` object for performing matching operations on the input string: ## Example Matcher matcher = pattern.matcher("aaaab"); #### `matches(String regex, CharSequence input)` Static method that directly determines whether the input string completely matches the regular expression: ## Example boolean isMatch = Pattern.matches("a*b", "aaaab");// returns true #### `split(CharSequence input)` Splits the input string based on the regular expression: ## Example String[] parts = pattern.split("aaaabtest");// returns ["", "test"] * * * ## Regular Expression Flags When compiling regular expressions, different flags can be specified to change matching behavior: | Flag | Description | | --- | --- | | `Pattern.CASE_INSENSITIVE` | Case-insensitive matching | | `Pattern.MULTILINE` | Multiline mode, `^` and `$` match the beginning and end of each line | | `Pattern.DOTALL` | `.` matches all characters, including line terminators | | `Pattern.UNICODE_CASE` | Enables Unicode-aware case folding | Usage example: ## Example // Case-insensitive matching Pattern pattern = Pattern.compile("a*b", Pattern.CASE_INSENSITIVE); * * * ## Practical Applications of Pattern Class ### Validating Email Format ## Example String emailRegex ="^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$"; Pattern emailPattern = Pattern.compile(emailRegex); String email ="test@example.com"; boolean isValid = emailPattern.matcher(email).matches(); ### Extracting Numbers ## Example String text ="Price: $123.45"; Pattern numberPattern = Pattern.compile("\d+\.?\d*"); Matcher matcher = numberPattern.matcher(text); if(matcher.find()){ String number = matcher.group();// "123.45" } ### Replacing Strings ## Example String input ="Hello, my phone is 123-456-7890"; Pattern phonePattern = Pattern.compile("\d{3}-\d{3}-\d{4}"); String output = phonePattern.matcher(input).replaceAll(""); // Output: "Hello, my phone is " * * * ## Performance Optimization Suggestions 1. **Pre-compile regular expressions**: For regular expressions that need to be used multiple times, they should be pre-compiled into `Pattern` objects rather than recompiling each time they are used. 2. **Avoid overly complex regular expressions**: Excessively complex regular expressions may lead to performance degradation or even "catastrophic backtracking" issues. 3. **Use groups reasonably**: Non-capturing groups `(?:...)` perform better than capturing groups `(...)`. If capturing content is not needed, non-capturing groups should be used. * * * ## Summary The `Pattern` class is the core of Java's regular expression functionality, providing powerful string pattern matching capabilities. By reasonably using the `Pattern` class and its accompanying `Matcher` class, various string processing tasks can be completed efficiently. Mastering the use of regular expressions and the `Pattern` class will greatly enhance a developer's text processing capabilities. The following table lists the commonly used methods of the `Pattern` class: ### Construction and Compilation Methods | Method | Description | | --- | --- | | `static Pattern compile(String regex)` | Compiles a regular expression into a Pattern object | | `static Pattern compile(String regex, int flags)` | Compiles a regular expression with specified flags | | `static boolean matches(String regex, CharSequence input)` | Quickly matches a regular expression (compile + match) | | `static String quote(String s)` | Converts a string to a literal pattern (escapes all special characters) | ### Flag Constants (Commonly Used) | Flag | Description | | --- | --- | | `Pattern.CASE_INSENSITIVE` | Case-insensitive matching | | `Pattern.MULTILINE` | Multiline mode (^ and $ match line beginning and end) | | `Pattern.DOTALL` | Dot (.) matches all characters including line terminators | | `Pattern.UNICODE_CASE` | Enables Unicode-aware case folding | ### Matching and Splitting Methods | Method | Description | | --- | --- | | `Matcher matcher(CharSequence input)` | Creates a matcher object | | `String[] split(CharSequence input)` | Splits input string using the pattern | | `String[] split(CharSequence input, int limit)` | Splits with a limit on the number of splits | | `Stream splitAsStream(CharSequence input)` | Returns a stream of split results (Java 8+) | ### Pattern Information Methods | Method | Description | | --- | --- | | `String pattern()` | Returns the compiled regular expression | | `int flags()` | Returns the flags set during compilation | | `String toString()` | Returns the string representation of the pattern |
← Java Date ClassJava Nio File Probecontenttype β†’