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

    xiaoxiao2024-12-26  66

    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)