Java Regex
Regular Expressions, or Regex for short, is a Java API that allows users to define String patterns for searching, manipulating, and modifying strings. It is commonly used for setting limits on various areas of strings such as email validation and passwords. The java.util.regex package contains regular expressions and consists of three classes and one interface. The following table lists the three classes included in the java.util.regex package:
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: This class does not have any public constructors. Instead, it consists of a group of regular expressions that can be utilized to define different types of patterns. To do this, simply execute the compile() method with a regular expression as the first input. After execution, the method will return a pattern.
The table below provides a list of the methods in this class along with their descriptions.
Method | Description |
---|---|
compile(String regex) | Compiles a regular expression into a pattern. |
compile(String regex, int flags) | Turns a regular expression into a pattern using the flags provided. |
flags() | Gets the match flags for this pattern. |
matcher(CharSequence input) | Builds a matcher that compares the given input to the pattern. |
matches(String regex, CharSequence input) | Matches a regular expression and tries to match it against the given input. |
pattern() | Gets the regular expression that was used to create this pattern. |
quote(String s) | Generates a literal pattern String from the given String. |
split(CharSequence input) | Splits the given input sequence around patterns that match this pattern. |
split(CharSequence input, int limit) | Divides 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() | Returns the pattern’s string representation. |
Matcher Class: To evaluate previously described patterns through match operations on an input string in Java, the ‘object’ is utilized. No public constructors are defined here. One can execute a matcher() on any pattern object to achieve this. The table below displays the methods present in this class along with their descriptions:To evaluate previously described patterns through match operations on an input string in Java, the ‘object’ is utilized. No public constructors are defined here. One can execute a matcher() on any pattern object to achieve this.
The table below displays the methods present in this class along with their descriptions:
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. |
Java Cheat Sheet
Java is a programming language and platform that has been widely used since its development by James Gosling in 1982. It follows the Object-oriented Programming concept and can run programs written in any programming language. Java is a high-level, object-oriented, secure, robust, platform-independent, multithreaded, and portable programming language All those words are collectively called Java Buzzwords. It is commonly used for programming web-based, window, enterprise, and mobile applications. This Java Cheat Sheet article has been written by experts in Java and based on the experience of students who have recently undergone Java interviews.
This Core Java Cheat Sheet has been designed by Java experts, based on the experience of students who have recently undergone Java interviews. Whether you are a beginner or an experienced Java developer, this Java Cheat Sheet is a valuable resource for quickly accessing essential syntax, concepts, and best practices related to Java Programming.
Contact Us