正则表达式(十六)——限定符

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/jikefzz1095377498/article/details/82782971
package com.wy.regular;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RegularTest {

	public static void main(String[] args) {
		/**
		 *  quantifiers 修饰符,限定符
		 *  Greedy quantifiers 贪婪的
		 *  Reluctant quantifiers 勉强的,不情愿的。比Greedy quantifiers多了一个?
		 *  Possessive quantifiers 独占性的。比Greedy quantifiers多了一个+
		 */
		// 1、Pattern pattern = Pattern.compile("(.{3,10})[0-9]"); 
		// 2、Pattern pattern = Pattern.compile("(.{3,10}?)[0-9]");
		Pattern pattern = Pattern.compile("(.{3,10}+)[0-9]");
		String str = "aaaa5bbbb6";
		Matcher matcher = pattern.matcher(str);
		if (matcher.find()) 
			/*
			 * 1:0-10 直接分析10个字符
			 * 2:0-5 aaaa5, 先分析3个字符
			 */
			pri(matcher.start() + "-" + matcher.end());
		else 
			pri("not match!");// 3:not match!,用的比较少,不会去掉分析过的。追求效率的时候可以用第三个
	}

	private static void pri(String str) {
		System.out.println(str);
	}
}

猜你喜欢

转载自blog.csdn.net/jikefzz1095377498/article/details/82782971
今日推荐