1. What are Character Classes?
If you read the definition of the Pattern class, you will see that there is a table summarizing the types of regular expression constructs supported by the Pattern class, the following is an excerpt from it.
Construct | Description | translate |
---|---|---|
[abc] | a, b, or c (simple class) | a, b or c (simple types) |
[^abc] | Any character except a, b, or c (negation) | Handle any character except a, b and c (negation) |
[a-zA-Z] | a through z, or A through Z, inclusive (range) | a ~ z or characters in A~Z (range) |
[a-d[m-p]] | a through d, or m through p: [a-dm-p] (union) | a ~d or m ~p (joint) |
[a-z&&[def]] | d, e, or f (intersection) | The range of a~z is either d, or e, or f, so it can only be d, e and f in combination (intersection) |
[a-z&&[^bc]] | a through z, except for b and c: [ad-z] (subtraction) | a ~ z and cannot be b or c (difference) |
[a-z&&[^m-p]] | a through z, and not m through p: [a-lq-z] (subtraction) | a ~ z and cannot be a character between m~p (difference set) |
The understanding of classes , the classes here should not be confused with the classes in the java language. The classes are under the semantics of regular expressions. Character classes are strings enclosed in square brackets. These strings are used to match strings with matching strings. characters in .
Below we will experiment with each of these six types:
2 experiments
2.1 simple classes (simple types)
2.2 negation
2.3 Ranges
2.4 Unions
2.5 Intersections
2.6 Subtraction
Previous section: [Java Regular Expression Series] 3. String