Answer» Regex (short for Regular Expressions) is a Java API for defining String patterns that may be used to search, manipulate, and modify strings. Regex is frequently used to specify the limits in a number of areas of strings, including email validation and passwords. The java.util.regex package contains regular expressions. This is made up of three classes and one interface. The java.util.regex package consists of the following three classes, which are listed below in the tabular format:
| Class | Description |
|---|
| util.regex.Pattern | It is used to define patterns. | | util.regex.Matcher | It is used to conduct match operations on text using patterns. | | PatternSyntaxException | In a regular expression pattern, it's used to indicate a syntax problem. |
Pattern class: There are no public constructors in this class. It is a collection of regular expressions that can be used to define various types of patterns. This can be done by executing the compile() method, which takes a regular expression as its first input and returns a pattern after it has been executed. The following table lists the methods present in this class and their description:
| Method | Description |
|---|
| compile(String regex) | Its purpose is to compile a regular expression into a pattern. | | compile(String regex, int flags) | It's used to turn a regular expression into a pattern using the flags provided. | | flags() | It's used to get the match flags for this pattern. | | matcher(CharSequence input) | It's used to build a matcher that compares the given input to the pattern. | | matches(String regex, CharSequence input) | matches(String regex, CharSequence input) is a function that compiles a regular expression and tries to match it against the given input. | | pattern() | It's used to get the regular expression that was used to create this pattern. | | quote(String s) | It's used to generate a literal pattern String from the given String. | | split(CharSequence input) | This splits the given input sequence around patterns that match this pattern. | | split(CharSequence input, int limit) | It's used to divide the given input sequence into sections based on matches to this pattern. The number of times the pattern is applied is controlled by the limit parameter. | | toString() | It's used to return the pattern's string representation. |
Matcher Class: This object is used to evaluate the previously described patterns by performing match operations on an input string in Java. There are no public constructors defined here either. Invoking a matcher() on any pattern object can be used to accomplish this. The following table lists the methods present in this class and their description:
| Method | Description |
|---|
| find() | find() is mostly used to look for multiple occurrences of regular expressions in a text. | | find(int start) | It is used to find occurrences of regular expressions in the text starting from the provided index. | | start() | start() is used to retrieve the start index of a match found with the find() method. | | end() | It's used to acquire the end index of a match discovered with the find() method. | | groupCount() | groupCount() is a function that returns the total number of matched subsequences. | | matches() | It's used to see if the pattern matches the regular expression. |
|