Java-Using Escapes

Using Escapes

In regular expressions, certain characters have special meaning. This leads to the question, what if you want to search for one of those special characters? In that case, you escape the character by preceding it with a backslash. For example, in the following program, \ \( represents a left parenthesis, and \\) represents a right parenthesis. Without the backslashes, the regular expression treats the parenthesis as a grouping element.


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("\\([0-9]{3}\\)");
        Matcher mat = pat.matcher("(455)789(677)89");

        int i = 0;
            System.out.println(i+"th subsequence  : ";

Leave a Comment