
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
Explain character classes in Java regular expressions
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].
- Related Articles
- Character class: intersection - Java regular expressions
- Character class: subtraction - Java regular expressions
- Character class: Negation - Java regular expressions
- Character class: range - Java regular expressions
- Character class: union - Java regular expressions
- What are negated character classes that are used in Python regular expressions?
- Explain quantifiers in Java regular expressions
- Use a character class in Java Regular Expressions
- Explain the sub-expression "[...]" in Java Regular expressions
- Explain the Metacharacter "B" in Java Regular Expressions.
- Explain the Sub-Expression "(?> re)" in Java Regular Expressions
- Explain about regular expressions in TOC?
- What are character classes or character sets used in Python regular expression?
- What are repeating character classes used in Python regular expression?
- Java Regular Expressions Tutorial
