A regular expression contains a string of characters with special meanings. These special characters are called metacharacters in the regular expression.
For example: d in \dcat is a metacharacter with special meaning, representing any one from 0 to 9.
The strings Ocat, 1cat, 2cat, ..., 9cat are all strings that match the regular expression \dcat.
The string object calls the public boolean matches(String regex) method to determine whether the current string object matches the regular expression specified by the parameter regex.
Commonly used metacharacters are as follows:
In regular expressions, you can use square brackets to enclose several characters to represent a metacharacter, which represents any character in the square brackets, for example:
regex=[159]ABC;
Then, 1ABC, 5ABC and 9ABC are all strings that match the regular expression regex.
For example:
[abc] represents any one of a, b, c.
[^abc] represents any character except a, b, c.
[a-zA-Z] represents any of the English letters (including uppercase and lowercase).
[ad] represents any one from a to d.
In addition, nested square brackets are allowed within square brackets, and union, intersection, and difference operations can be performed, for example:
[ad[mp]] represents any character (and) from a to d or m to p.
[az&&[def]] represents any one of d, e, or f (cross).
[af&&[^bc]] represents a, d, e, f (difference).
Note : Since "." represents any character, if you want to use a common dot character in a regular expression, you must use [.] or 56 to represent a common dot character.
Qualifier modifiers can be used in regular expressions.
For example, for the qualification modifier?, if X represents a metacharacter or an ordinary character in the regular expression, then X? means that X appears 0 or 1 times, for example:
regex=hel1o[2468]?;
Well, hello, hello2, hello4, hello6 and hello8 are all strings that match the regular expression regex.
Commonly used qualification modifiers are used as follows:
For example:
regex=@\w{4};
Then, @abcd, @Java, and @知行合一 are all strings that match the regular expression regex.
Note : For details about regular expressions, check out the Pattern class in the java.util.regex package.