[Java Regular Expression Series] 4. Character Classes

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)

write picture description here

write picture description here

write picture description here

write picture description here

2.2 negation

write picture description here

write picture description here

2.3 Ranges

write picture description here

write picture description here

2.4 Unions

write picture description here

write picture description here

write picture description here

2.5 Intersections

write picture description here

write picture description here

2.6 Subtraction

write picture description here

write picture description here

write picture description here

Previous section: [Java Regular Expression Series] 3. String

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324853825&siteId=291194637