使用Pattern查找字符串


      Scanner scanner = new Scanner(System.in);
        while (true) {
            System.out.print("Enter your regex: ");
            Pattern pattern = Pattern.compile(scanner.nextLine());
            System.out.print("Enter input string to search: ");
            Matcher matcher = pattern.matcher(scanner.nextLine());
            boolean found = false; 
            while (matcher.find()) {
                System.out.println("Found \""+matcher.group()+
                        "\" starting index "+matcher.start()+
                        " ending index "+matcher.end());
                found = true;
            }
            if (!found) {
                System.out.println("No match found.");
            }           
        }

你可能感兴趣的:(regex)