首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >java之学习正则Greedy 数量词的用法

java之学习正则Greedy 数量词的用法

作者头像
吾爱乐享
发布2018-07-13 12:02:40
发布2018-07-13 12:02:40
5340
举报
文章被收录于专栏:吾爱乐享吾爱乐享

结果示意图

Greedy 数量词

* X? X,一次或一次也没有 * X* X,零次或多次 * X+ X,一次或多次 * X{n} X,恰好 n 次 * X{n,} X,至少 n 次 * X{n,m} X,至少 n 次,但是不超过 m 次

案例代码

代码语言:javascript
复制
package com.ifenx8.regex;

import javax.print.DocFlavor.STRING;

public class Demo4_Regex {

	/**
	 * A:Greedy 数量词 
		* X? X,一次或一次也没有
		* X* X,零次或多次
		* X+ X,一次或多次
		* X{n} X,恰好 n 次 
		* X{n,} X,至少 n 次 
		* X{n,m} X,至少 n 次,但是不超过 m 次 
	 */
	public static void main(String[] args) {
		demo1();//	X? X,一次或一次也没有
		demo2();//	X* X,零次或多次
		demo3();//	X+ X,一次或多次
		demo4();//	X{n} X,恰好 n 次 
		demo5();//	X{n,} X,至少 n 次 
		String regex = "[abc]{5,14}";//出现5次到14次之间
		System.out.println("abcac".matches(regex));//出现5次,返回true
		System.out.println("abcacaaabbbccc".matches(regex));//出现大于等于5次小于等于14次,返回true
		System.out.println("abcc".matches(regex));//出现4次 不等于5次,返回false
		System.out.println("asdfb".matches(regex));//虽然有5个字符 但是有不包含其中的字符,返回false
		System.out.println("=========");
	}

	public static void demo5() {
		String regex = "[abc]{5,}";//出现5次及5次以上
		System.out.println("abcac".matches(regex));//出现5次,返回true
		System.out.println("abcacaaabbbccc".matches(regex));//出现大于5次,返回true
		System.out.println("abcc".matches(regex));//出现4次 不等于5次,返回false
		System.out.println("asdfb".matches(regex));//虽然有5个字符 但是有不包含其中的字符,返回false
		System.out.println("=========");
	}

	public static void demo4() {
		String regex = "[abc]{5}";//刚好出现5次
		System.out.println("abcac".matches(regex));//出现5次,返回true
		System.out.println("abcacaaabbbccc".matches(regex));//出现大于5次,返回false
		System.out.println("abcc".matches(regex));//出现4次 不等于5次,返回false
		System.out.println("asdfb".matches(regex));//虽然有5个字符 但是有不包含其中的字符,返回false
		System.out.println("=========");
	}

	public static void demo3() {
		String regex = "[abc]+";//表示出现一次或者多次
		System.out.println("a".matches(regex));//出现一次,返回true
		System.out.println("".matches(regex));//出现0次,返回false
		System.out.println("bbbbccc".matches(regex));//出现多次,返回true
		System.out.println("d".matches(regex));//不出现,返回false
		System.out.println("==========");
	}

	public static void demo2() {
		String regex = "[abc]*";//表示出现0次或者多次
			System.out.println("aaaccb".matches(regex));//出现多次,返回true
			System.out.println("a".matches(regex));//出现一次,返回true
			System.out.println("".matches(regex));//出现0次,返回true
			System.out.println("d".matches(regex));//没有出现,返回false
			System.out.println("=========");
	}

	public static void demo1() {
		String regex ="[abc]?";//表示在abc中的一个或者一个也没有
		System.out.println("a".matches(regex));//true
		System.out.println("b".matches(regex));//true
		System.out.println("c".matches(regex));//true
		System.out.println("d".matches(regex));//false
		System.out.println("".matches(regex));//true
		System.out.println("================");
	}

}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2017-08-28 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 结果示意图
  • Greedy 数量词
  • 案例代码
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档