测试的结果作为注释放在测试方法的后面。
package regularexpression;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.junit.Test;
/**
* 测试字符类;
* @author yuncong
*
*/
public class TestRegularExpression {
/**
* 0. 单个字符匹配其本身
*/
@Test
public void test0() {
String regex = "d";
String input = "1B2s-[23d@3^";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(input);
while (matcher.find()) {
String output = matcher.group();
System.out.println(output);
}
}
/**
* 输出:
* d
*/
/**
* 1. [C1C2...],表示任何C1,C2,...中的某个字符,
* 其中Ci可以是是多个字符(如aB)、字符范围(如A-Z、
* a-z、0-9)或字符类(如[A-Z]、\d)
*/
@Test
public void test1() {
// 多个字符
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);
}
}
/**
* 输出:
* d
*/
@Test
public void test2() {
// 字符范围
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);
}
}
/**
* 输出:
* B
* s
* d
*/
@Test
public void test3() {
// 字符类
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);
}
}
/**
* 输出:
* 1
* B
* 2
* s
* 2
* 3
* d
* 3
*/
/**
* 2. [^...],表示字符类的补集中的某个字符
*/
@Test
public 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);
}
}
/**
* 输出:
* 1
* 2
* 2
* 3
* 3
*/
/**
* 如果字符类包含^,^可以放在除开始位置之外的任何位置,
* 也就是说,只有当^放在开始位置时,才具有取补的功能
*/
@Test
public void test5() {
// 第二个^就是一个普通的匹配字符
String regex = "[^0-9^]";
String input = "1B2s2^3d@^3";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(input);
while (matcher.find()) {
String output = matcher.group();
System.out.println(output);
}
}
/**
* 输出:
* B
* s
* d
* @
*/
/**
* 如果字符类包含-,-必须是第一项或者最后一项
*/
@Test
public void test6() {
String regex = "[-0-9]";
String input = "1B2s-23d@3^";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(input);
while (matcher.find()) {
String output = matcher.group();
System.out.println(output);
}
}
/**
* 输出:
* 2
* -
* 2
* 3
* 3
*/
/**
* 如果字符类中包含[,必须放在第一项并且转义
*/
@Test
public void test7() {
String regex = "[\\[0-9]";
String input = "1B2s-[23d@3^";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(input);
while (matcher.find()) {
String output = matcher.group();
System.out.println(output);
}
}
/**
* 输出:
* 1
* 2
* [
* 2
* 3
* 3
*/
/**
* 3. [...&&...],两个字符类的交集
*/
@Test
public void test8() {
String regex = "[abcd&&a-z]";
String input = "1B2s-[23d@3^";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(input);
while (matcher.find()) {
String output = matcher.group();
System.out.println(output);
}
}
/**
* 输出:
* d
*/
/**
* 4. \w, 一个词语字符类(也就是一个单词中可能存在的字符),
* 等价于[A-Za-z0-9_]
*/
@Test
public void test9() {
String regex = "\\w";
String input = "1B2s-[23d李@3^";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(input);
while (matcher.find()) {
String output = matcher.group();
System.out.println(output);
}
}
/**
* 输出:
* 1
* B
* 2
* s
* 2
* 3
* d
* 3
*/
/**
* 5. \p{name}, 表示一个命名字符类
*/
@Test
public void test10() {
String regex = "\\p{Upper}";
String input = "1B2s-[23d李@3^";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(input);
while (matcher.find()) {
String output = matcher.group();
System.out.println(output);
}
}
/**
* 输出:
* B
*/
}