Explain character classes in Java regular expressions

JavaObject Oriented ProgrammingProgramming

The character classes in Java regular expression is defined using the square brackets "[ ]", this subexpression matches a single character from the specified or, set of possible characters.

For example the regular expression [abc] matches a single character a or, b or, c. Similarly, "[a-z]" matches a single character from a to z.

Following are other variants of character Java regex classes:

Negation − The negation variant of the character class is defined as "[^ ]" (with ^ within the square braces), it matches a single character which is not in the specified or set of possible characters.For example the regular expression [^abc] matches a single character except a or, b or, c. Similarly, "[^a-z]" matches a character excepts alphabets from a to z.

Range − The range variant of the character class allows you to use a range of characters.For example, the expression [a-z] matches a single character from the alphabets a to z and the expression [^A-Z] matches a character which is not a capital letter.

Union − The union variant of the character class allows you to match a character from one of the specified ranges.For example, the expression [a-z[0-9]] matches a single character which is either a small alphabet (a-z) or a digit (0-9).

Intersection − The intersection variant of the character class allows you to match a character which is common in the ranges that have intersection relation between them. An intersection relation between ranges is defined using &&.For example, the expression [a-z&&[r-u]] matches a single character from r to u.

Subtraction − You can subtract one range from other and use it as new range. You can achieve this by using two variants of character classes i.e. negation and intersection.For example the intersection of ranges [a-l] and [^e-h] gives you the characters a to l as rage subtracting the characters [e-h].

raja
Published on 13-Jan-2020 10:23:50
Advertisements