java使用Matcher

java使用Matcher

判断字符串是否匹配正则

boolean matches = Pattern.matches("(\\d{4})-((\\d{2})-(\\d{2}))", "2017-04-25");

Matcher的用法

案例代码


	public static final String DATE_STRING = "2017-04-25";
	/**
	 * 普通捕获,使用左括号来分组
	 */
	public static final String P_COMM = "(\\d{4})-((\\d{2})-(\\d{2}))";
	/**
	 * 使用名字捕获,使用 ? 来捕获,同时这个不影响使用group组来捕获,一个左括号仍然代表一组
	 */
	public static final String P_NAMED = "(?\\d{4})-(?(?\\d{2})-(?\\d{2}))";
	/**
	 * 反捕获,使用符号 ?: 紧跟在左括号( 的后面,表示忽略这个左括号包含的部分
	 */
	public static final String P_UNCAP = "(?:\\d{4})-((\\d{2})-(\\d{2}))";
	public static void main(String[] args) {

		Pattern patternByComm = Pattern.compile(P_COMM);
		Matcher matcherByComm = patternByComm.matcher(DATE_STRING);
		boolean matches = Pattern.matches("(\\d{4})-((\\d{2})-(\\d{2}))", "2017-04-25");
		matcherByComm.find();//必须要有这句
		System.out.println("================================");
		System.out.println("普通捕获组");
		System.out.printf("matcher.group(0) value:%s", matcherByComm.group(0));
		System.out.printf("\nmatcher.group(1) value:%s", matcherByComm.group(1));
		System.out.printf("\nmatcher.group(2) value:%s", matcherByComm.group(2));
		System.out.printf("\nmatcher.group(3) value:%s", matcherByComm.group(3));
		System.out.printf("\nmatcher.group(4) value:%s", matcherByComm.group(4));
		System.out.println("");
		System.out.println("================================");

		Pattern patternByName = Pattern.compile(P_NAMED);
		Matcher matcherByName = patternByName.matcher(DATE_STRING);
		matcherByName.find();
		System.out.printf("\n===========使用名称获取=============");
		System.out.printf("\nmatcher.group(0) value:%s", matcherByName.group(0));
		System.out.printf("\n matcher.group('year') value:%s", matcherByName.group("year"));
		System.out.printf("\nmatcher.group('md') value:%s", matcherByName.group("md"));
		System.out.printf("\nmatcher.group('month') value:%s", matcherByName.group("month"));
		System.out.printf("\nmatcher.group('date') value:%s", matcherByName.group("date"));
		System.out.println("================================");
		System.out.println("");
		matcherByName.reset();
		System.out.print("\n===========使用编号获取=============");

		matcherByName.find();
		System.out.printf("\nmatcher.group(0) value:%s", matcherByName.group(0));
		System.out.printf("\nmatcher.group(1) value:%s", matcherByName.group(1));
		System.out.printf("\nmatcher.group(2) value:%s", matcherByName.group(2));
		System.out.printf("\nmatcher.group(3) value:%s", matcherByName.group(3));
		System.out.printf("\nmatcher.group(4) value:%s", matcherByName.group(4));
		System.out.println("================================");
		System.out.println("");

		System.out.print("\n===========反捕获=============");
		Pattern patternUncap = Pattern.compile(P_UNCAP);
		Matcher matcherUncap = patternUncap.matcher(DATE_STRING);
		matcherUncap.find();
		System.out.printf("\nmatcher.group(0) value:%s", matcherUncap.group(0));
		System.out.printf("\nmatcher.group(1) value:%s", matcherUncap.group(1));
		System.out.printf("\nmatcher.group(2) value:%s", matcherUncap.group(2));
		System.out.printf("\nmatcher.group(3) value:%s", matcherUncap.group(3));


	}
输出结果
普通捕获组
matcher.group(0) value:2017-04-25
matcher.group(1) value:2017
matcher.group(2) value:04-25
matcher.group(3) value:04
matcher.group(4) value:25

===========使用名称获取=============
matcher.group(0) value:2017-04-25
 matcher.group('year') value:2017
matcher.group('md') value:04-25
matcher.group('month') value:04
matcher.group('date') value:25

===========使用编号获取=============
matcher.group(0) value:2017-04-25
matcher.group(1) value:2017
matcher.group(2) value:04-25
matcher.group(3) value:04
matcher.group(4) value:25

===========反捕获=============
matcher.group(0) value:2017-04-25
matcher.group(1) value:04-25
matcher.group(2) value:04
matcher.group(3) value:25

你可能感兴趣的:(java,java)