![]() |
VOOZH | about |
Regular Expressions in Java provide a way to match, search, and manipulate text. One of the most important building blocks of regex is the Character Class, which allows you to match one character from a defined set of characters.
true
Explanation:
[characters]
A simple character class matches exactly one character from the given set inside square brackets. It is used when you want to allow multiple specific characters at a single position
true true false
Explanation:
Range Character Class matches one character within a specified range using a hyphen (-). It is commonly used to validate digits, lowercase letters, or uppercase letters efficiently.
true false
Explanation:
Multiple Range Character Class allows combining several character ranges in one set. This is useful when validating inputs that can contain letters and digits together.
true true false
Explanation:
A Negated Character Class is used to match any single character except the ones specified inside the brackets. It is identified by the caret (^) placed at the beginning of the character set.
true false
Explanation:
Java provides predefined character classes for commonly used patterns.
Regex | Description |
|---|---|
\d | Digit [0-9] |
\D | Non-digit |
\w | Word character [a-zA-Z0-9_] |
\W | Non-word character |
\s | Whitespace (space, tab, newline) |
\S | Non-whitespace |
Quantifiers are used to specify how many times a character from a defined set or range should appear. They make regex patterns more powerful for validating repeated characters like digits, letters, or symbols.
Quantifier | Meaning | Description | Example |
|---|---|---|---|
* | Zero or more times | Allows any number of occurrences of a character class | [0]*[0-9] |
+ | One or more times | Requires at least one occurrence of a character class | [0]+[0-9] |
? | Zero or one time | Makes a character class optional | [A-Za-z][A-Za-z]-?[0-9][0-9] |
{m} | Exactly m times | Matches a character class exactly m times | [0-9]{3} |
{m,} | At least m times | Matches a character class m or more times | [0-9]{2,} |
{m,n} | At least m and at most n times | Matches a character class between m and n times | [0-9]{2,4} |
Note:
- Quantifiers always apply to the preceding character class
- [0-9]{3} -> digit class repeated 3 times
- -? -> optional hyphen using quantifier on a character