Java regular expressions are unique series of characters, which assists in matching or locating other strings or collection of strings, with a unique syntax applied in a pattern. Java offers a regex package for matching with regular expressions.
The java.util.regex package mainly is composed of 3 classes.
- Pattern Class
A Pattern element is a compiled form of a regular expression. Pattern class does not offer public constructors. It is used for defining a pattern for the regex mechanism. The following compile methods permit a regular expression as the primary argument.
- static Pattern compile(String regex) piles up the specified regex and sends the instance of pattern as response.
- Matcher matcher(CharSequence input) builds a matcher that equals the specified input to the pattern.
- static boolean matches(String regex, CharSequence input) acts as the blend of compile and matcher methods.
- String[] split(CharSequence input) divides the input string between matches of specified pattern.
- String pattern() delivers the regex pattern.
- Matcher Class
A Matcher element understands the pattern and then executes matching actions against an input string. Matcher class does not offer public constructors. It executes Match Result interface. The matcher methods are:
- boolean matches() checks if the regular expression equals the pattern.
- boolean find() locates the subsequent expression that equals the pattern.
- boolean find(int start) locates the upcoming expression that equals the pattern from the specified start number.
- String group() delivers the equated subsequence.
- int start() delivers the first index of the equated subsequence.
- int end() delivers the final index of the equated subsequence.
- int groupCount() delivers the total of the equated subsequence.
- PatternSyntaxException
A PatternSyntaxException element is an unverified exception that points out syntax mistakes in a regular expression pattern. The class methods are:
- public String getDescription() fetches the report of the mistakes.
- public int getIndex() fetches the fault index.
- public String getPattern() fetches the invalid regular expressions.
- public String getMessage() delivers a multi-line string that contains (i) the report of the error and index, (ii) the invalid pattern, and (iii) a visual sign of the fault index inside the pattern.
Java Regular Expressions Example
In the below example, the regular expressions “.x”, “.x.”, and “..x” are used for finding the occurrence of the string in the given words. Here, a dot signifies a single character. The regular expressions can be written in 3 approaches.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
import java.util.regex.Matcher; import java.util.regex.Pattern; public class RGEXP { public static void main(String args[]) { // Way-1 Pattern s = Pattern.compile(".x"); Matcher a = s.matcher("ox"); boolean y = a.matches(); // Way-2 boolean y2 = Pattern.compile(".x.").matcher("oxe").matches(); // Way-3 boolean y3 = Pattern.matches("..x", "xx"); System.out.println("Regular expression testing " + y + " " + y2 + " " + y3); } } |
Output for the above program would be
Regular expression testing true true false
In the above example,
• The 1st approach is true as 2nd character is x.
• The 2nd approach is true as 2nd character is x.
• The 3rd approach output is false as it requests for 3rd character and “xx” has only 2 characters.
Leave a Reply