Java RegEx

Java RegEx , Regular Expressions is a search pattern that contains sequence of characters. RegEx is used to search for words or characters in a text body. User creates specific search patterns to get the desired data. RegEx perform the process of searching and replacing the data.

To create a Java Regex, user can use the package java.util.regex that contains the following classes:

  • Pattern: This class defines a pattern that can be used in searching. Here is a list of methods and their descriptions provided by this class:
1.Pattern compile(String regex)It compiles the given Regex and returns it as a pattern.
2.Matcher matcher(char string)It creates a matches that matches the specified string with the pattern.
3.boolean matches(String regex, char stirng)It both compiles and matches the Regex with the specified text.
4.split(char string)It splits the specified string based on the matched regex.
5.String pattern()It returns the RegEx as a pattern.
Java RegEx Pattern
  • Matcher: This class searches for and finds the matching pattern. Here is a list of methods and their descriptions provided by this class:
1.boolean matches()Checks if the RegEx matches the pattern or not. Returns true if does and false if it does not.
2.boolean find()Finds the next expression that matches the RegEx and returns true or false accordingly.
3.String group()Returns the sequence matched with the RegEx. start()Returns the first index of matching sequence. end()Returns the last index of matching sequence. groupCount()Returns the total count of matching sequences.
Java RegEx Matcher
  • PatternSyntaxException: This class shows syntax error in the regular expression pattern.

Here is an example, that shows how to check if the match exists in a text or not. The program uses the classes Matcher and Pattern. The Patter class defines a pattern that needs to be searched. While, the matcher class searches and checks whether the match exits or not.

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Example {
  public static void main(String[] args) {
    Pattern pattern = Pattern.compile("Tutorialsart", Pattern.CASE_INSENSITIVE);
    Matcher matcher = pattern.matcher("Welcome to Tutorialsart!");
    boolean matchFound = matcher.find();
    if(matchFound) {
      System.out.println("Match found in the text");
    } else {
      System.out.println("Match not found in the text");

In the above code, the Pattern.compile() method is used to create a pattern. It takes two parameters. Pattern is simply a string. The user is searching for the pattern “Tutorialsart” that is the first argument. The second argument Pattern.CASE_INSENSITIVE that defines that the pattern is case sensitive. In case same text is found but in different case then it cannot be a match.

From the Matcher class, the matcher() method matches the pattern in the string. The find() method returns true if the match exits or returns false if match does not exist.

The output of the above code after compilation is:

Java RegEx Syntax

Regex is composed of metacharacters and quantifiers. Here is list of metacharacters allowed by Java syntax. Their description is also given in the table:

1.|Finds the match from one of the sequences separated by |. For example, finds a match in either a or b in a|b.
2..Finds one match for any single character.
3.$Finds a match at the end of line. For example, finds a match after the text “java” in java$.
4.^Finds a match in the beginning of line. For example, finds a match before the text “java” in ^java.
5.[…]Finds a match for any single character between the square brackets.
6.[^…]Finds a match for single character not present in the square brackets.
7.\AFinds a match in the beginning of the string.
8.\bFinds a match of word boundaries when written outside the brackets. Finds a match of backspaces when written inside the brackets.
9.\BFinds a match for nonword boundaries.
10.\dFinds digits from 0-9.
11.\DFinds nondigits.
12.\EEnds quotes that start with \Q.
13.\GKeeps a track of last match and finds a new match after that point.
14.\nFinds a match of the newline character.
15. \tFinds a match of new tab character.
16.\QEscapes the string in quotes.
17.\wFinds a match of word characters.
18.\WFinds a match of nonword characters.
19.\uxxxxFinds a match of unicode characters specified by xxxx.
Java RegEx Syntax

Java RegEx Quantifiers

The Java RegEx quantifiers are used to define quantities.

1.r+Finds a match in any string that contains at least one r.
2.r*Finds a match in any string that contains at least one or more r.
3.r?Finds a match in any string that contains at least one r.
4.r{x}Finds a match in a sequence that contains x numbers of r occurrences.
5.r{x,}Finds a match in any sequence that contains at least x number of r occurrences.
6.r{x,y}Finds a match in a sequence that contains at least x and maximum y number of r occurrences.
Java RegEx Quantifiers