正则表达式--start end

前端之家收集整理的这篇文章主要介绍了正则表达式--start end前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class MainClass {
    public static void main(String[] args) {
        Pattern pattern = Pattern.compile("\\d{3,5}");
        String string = "123-4561-789-2fv";
        Matcher matcher = pattern.matcher(string);
        boolean result = matcher.matches();
        System.out.println(result);

        matcher.reset();
        System.out.println(matcher.find());
        System.out.println(matcher.start()+"-"+matcher.end());

        System.out.println(matcher.find());
        System.out.println(matcher.start()+"-"+matcher.end());

        System.out.println(matcher.find());
        System.out.println(matcher.start()+"-"+matcher.end());

        System.out.println(matcher.find());
        //System.out.println(matcher.start()+"-"+matcher.end());

        System.out.println(matcher.lookingAt());
        System.out.println(matcher.lookingAt());
        System.out.println(matcher.lookingAt());
        System.out.println(matcher.lookingAt());
    }
}

output:

false
true
0-3
true
4-8
true
9-12
false
true
true
true
true

猜你在找的正则表达式相关文章