8.第七节demo1--java里面的小应用

package com.zzh.day2;

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

public class demo1 {
	public static void main(String[] args){
		//匹配e_mail
		System.out.println("adafdasfdasfdasfdsa@fdafdasf.com".matches("[\\w[.-]]+@[\\w[.-]]+\\.[\\w]+"));
		Pattern pattern = Pattern.compile("\\d{3,5}");
		String string = "123-343456-234-000";
		Matcher matcher = pattern.matcher(string);
		System.out.println("1 "+matcher.matches());//输出是否匹配
		matcher.reset();
		System.out.println("2 "+matcher.find()+"   "+matcher.group()+"   "+matcher.start()+"   "+matcher.end());
		System.out.println("3 "+matcher.find()+"   "+matcher.group()+"   "+matcher.start()+"   "+matcher.end());
		System.out.println("4 "+matcher.find()+"   "+matcher.group()+"   "+matcher.start()+"   "+matcher.end());
		System.out.println("5 "+matcher.find()+"   "+matcher.group()+"   "+matcher.start()+"   "+matcher.end());
		matcher.reset();
		System.out.println("6 "+matcher.lookingAt());
		System.out.println("7 "+matcher.lookingAt());
		System.out.println("8 "+matcher.lookingAt());
		System.out.println("9 "+matcher.lookingAt());
	
	}
}

输出结果:
true
1 false
2 true 123 0 3
3 true 34345 4 9
4 true 234 11 14
5 true 000 15 18
6 true
7 true
8 true
9 true

/*
* matches() Attempts to match the entire region against the pattern.
* 尝试匹配整个区域
* find(),true if, and only if, a subsequence of the input sequence matches this matcher’s pattern
* 找到了可以匹配的字串
* lookingAt(),true if, and only if, a prefix of the input sequence matches this matcher’s pattern
* 意义跟matches相同,但每次配都是从头开始
* reset()Resets this matcher.
* 将匹配的位置重置为原始状态
* group()Returns the input subsequence matched by the previous match.
* 如英文解释说的一样,好好英语,真的很重要
* start()Returns the start index of the previous match.
* 某次成功匹配的起始位置,从0开始
* end()Returns the offset after the last character matched.
* 某次成功匹配的结束位置
* */

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值