
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Pattern Class in Java
Pattern class is represented as a compiled version of the regex pattern. The pattern class is provided in java.util.regex package. This class has various methods for various operations like matching, splitting, searching, etc. In order to create a pattern object a compile method is used.
Syntax
public static Pattern compile(String regex)
Here regex represents a regular expression which is a String. And to compile this we use this method. Moreover, this compiled object can be used to match a pattern using the Matcher method.
Algorithm
To compile and match a pattern, follow the steps below ?
Step 1 ? Initalize a regular expression pattern with String.
Step 2 ? Now, compile the pattern using compile method.
Step 3 ? Define the input string to match against the pattern.
Step 4 ? Create a Matcher object and apply the pattern to the input string.
Step 5 ? Use the Matcher method to perform various operations
Syntax
public class Regex { public static void main(String[] args) { String pattern = "String1"; Pattern compiledPattern = Pattern.compile(pattern); String input = "Strin2"; Matcher matcher = compiledPattern.matcher(input); if (matcher.find()) { System.out.println("Match found: " + matcher.group(0)); System.out.println("Captured group: " + matcher.group(1)); } else { System.out.println("No match found."); } } }
Approach 1: Using the matches() method
This approach involves using the matches() method.
Example
import java.util.regex.Pattern; public class MatchThePattern { public static void main(String[] args) { String pattern = "Hello (\w+)"; String input = "Hello World"; // Add the input string to be matched boolean letsMatch = Pattern.matches(pattern, input); if (letsMatch) { System.out.println("Pattern matched."); } else { System.out.println("Pattern not matched."); } } }
Outpu
Pattern matched.
Explanation
By passing in both string inputs as arguments to the matches method, we can successfully match two string patterns within the code presented above.
Approach 2: Using the find() method
The find() method returns a boolean type and finds the expression that matches the pattern. The following is a code example ?
Example
In this example, we will demonstrate Approach 2 by using the find() method.
import java.util.regex.Matcher; import java.util.regex.Pattern; public class LetsFindPattern { public static void main(String[] args) { String pattern = "\b\d+\b"; String input = "The price is 100 dollars for 3 items."; Pattern compiledPattern = Pattern.compile(pattern); Matcher matcher = compiledPattern.matcher(input); while (matcher.find()) { System.out.println("Match found: " + matcher.group()); } } }
Output
Match found: 100 Match found: 3
Comparison of Approach 1 and Approach 2
Criteria |
Approach 1 |
Approach 2 |
---|---|---|
Type |
String |
String |
Method |
boolean matches(String regex) |
boolean find() |
Method Logic |
Returns pattern if matched |
Returns matchning pattern |
Conclusion
A regular expression is used to match a pattern. The above approaches are examples of actions that should be taken to match a pattern. We also demonstrated these approaches with two working examples, showcasing their functionality and versatility. By understanding these methods and their use cases, you can efficiently find a matching pattern by using regular expressio