This article mainly tests the usage of the character class, and the result of the test is placed behind the test method as a comment.
Package Regularexpression;import Java.util.regex.matcher;import Java.util.regex.pattern;import org.junit.Test;/** * Test character class; * @author Yuncong * */public class Testregularexpression {/** * 0. A single character matches itself */@Testpublic void test0 () {String reg ex = "D"; String input = "1b2s-[[email protected]^"; Pattern pattern = pattern.compile (regex); Matcher Matcher = pattern.matcher (input), while (Matcher.find ()) {String output = Matcher.group (); SYSTEM.OUT.PRINTLN (output);}} /** * Output: * d *//** * 1. [C1c2 ...], which means any c1,c2,... One of the characters, * where CI can be multiple characters (such as AB), a character range (such as a-Z, * A-Z, 0-9) or a character class (such as [A-z], \d) */@Testpublic void test1 () {//multiple characters string regex = "[ABCD ]"; String input = "1B2S23D3"; Pattern pattern = pattern.compile (regex); Matcher Matcher = pattern.matcher (input), while (Matcher.find ()) {String output = Matcher.group (); SYSTEM.OUT.PRINTLN (output);}} /** * Output: * d */@Testpublic void Test2 () {//character range String regex = "[A-za-z]"; String input = "1B2S23D3"; Pattern pattern = pattern.compile (regex); Matcher Matcher = Pattern.matCher (input); while (Matcher.find ()) {String output = Matcher.group (); SYSTEM.OUT.PRINTLN (output);}} /** * Output: * B * S * d */@Testpublic void Test3 () {//character class String regex = "[[a-za-z][0-9]]"; String input = "1B2S23D3"; Pattern pattern = pattern.compile (regex); Matcher Matcher = pattern.matcher (input), while (Matcher.find ()) {String output = Matcher.group (); SYSTEM.OUT.PRINTLN (output);}} /** * Output: * 1 * B * 2 * S * 2 * 3 * d * 3 *//** * 2. [^ ...], which represents a character in the complement of a character class */@Testpublic void test4 () {String regex = "[^a-za-z]"; String input = "1B2S23D3"; Pattern pattern = pattern.compile (regex); Matcher Matcher = pattern.matcher (input), while (Matcher.find ()) {String output = Matcher.group (); SYSTEM.OUT.PRINTLN (output);}} /** * Output: * 1 * 2 * 2 * 3 * 3 *//** * If the character class contains ^,^ can be placed anywhere except the starting position, * that is, only if the ^ is placed at the start position, the function of the complement */@Testpublic void Test5 () { The second ^ is a generic match character string regex = "[^0-9^]"; String input = "1b2s2^[email protected]^3"; Pattern pattern = pattern.compile (regex); Matcher Matcher = pattern.matcher (input); WHIle (Matcher.find ()) {String output = Matcher.group (); SYSTEM.OUT.PRINTLN (output);}} /** * Output: * B * S * d * @ *//** * If the character class contains-,-must be the first or last item */@Testpublic void Test6 () {String regex = "[-0-9]"; String input = "[email protected]^"; Pattern pattern = pattern.compile (regex); Matcher Matcher = pattern.matcher (input), while (Matcher.find ()) {String output = Matcher.group (); SYSTEM.OUT.PRINTLN (output);}} /** * Output: * 2 *-* 2 * 3 * 3 *//** * If the character class contains [, must be placed in the first item and escaped */@Testpublic void Test7 () {String regex = "[\\[0-9]"; String input = "1b2s-[[email protected]^"; Pattern pattern = pattern.compile (regex); Matcher Matcher = pattern.matcher (input), while (Matcher.find ()) {String output = Matcher.group (); SYSTEM.OUT.PRINTLN (output);}} /** * Output: * 1 * 2 * [* 2 * 3 * 3 *//** * 3. [... &&], intersection of two character classes */@Testpublic void Test8 () {String regex = "[Abcd&&a-z]"; String input = "1b2s-[[email protected]^"; Pattern pattern = pattern.compile (regex); Matcher Matcher = pattern.matcher (input); (Matcher.find ()) {String output = Matcher.group (); SYSTEM.OUT.PRINTLN (output);}} /** * Output: * d *//** * 4. \w, a Word character class (that is, a character that may exist in a word), * is equivalent to [a-za-z0-9_]/@Testpublic void Test9 () {String regex = "\\w"; String input = "1b2s-[23d li @3^"; Pattern pattern = pattern.compile (regex); Matcher Matcher = pattern.matcher (input), while (Matcher.find ()) {String output = Matcher.group (); SYSTEM.OUT.PRINTLN (output);}} /** * Output: * 1 * B * 2 * S * 2 * 3 * d * 3 *//** * 5. \p{name}, which represents a named character class */@Testpublic void test10 () {String regex = "\\p{upper}"; String input = "1b2s-[23d li @3^"; Pattern pattern = pattern.compile (regex); Matcher Matcher = pattern.matcher (input), while (Matcher.find ()) {String output = Matcher.group (); SYSTEM.OUT.PRINTLN (output);}} /** * Output: * B */}
Java Test Regular expression (i)