The section of the input string matching the capturing group(s) is saved in memory for later recall via backreference. A backreference is specified in the regular expression as a backslash (\) followed by a digit indicating the number of the group to be recalled. For example, the expression (XX) defines one capturing group matching two XX in a row, which can be recalled later in the expression via the backreference \1.


Program Source

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

public class Javaapp {
    public static void main(String[] args) {
        Pattern pat = Pattern.compile("(XX)(YY)\\d{4}\\2\\1");
        Matcher mat = pat.matcher("XXYY1234YYXX1234");
        int i = 0;
            System.out.println(i+"th subsequence  : ";

Leave a Comment