集册 Java实例教程 正则表达式测试工具

正则表达式测试工具

欢马劈雪     最近更新时间:2020-01-02 10:19:05

495
正则表达式测试工具

import java.io.Console;

import java.util.regex.Pattern;

import java.util.regex.Matcher;
//来 自 时 代 J a v a - nowjava.com

public class RegexTestHarness {


    public static void main(String[] args) {

        Console console = System.console();

        if (console == null) {

            System.err.println("No console.");

            System.exit(1);

        }

        while (true) {


            Pattern pattern = Pattern.compile(console

                    .readLine("%nEnter your regex: "));


            Matcher matcher = pattern.matcher(console

                    .readLine("Enter input string to search: "));


            boolean found = false;

            while (matcher.find()) {
            /*
            来 自*
             时 代 J a v a - nowjava.com
            */

                console.format("I found the text" + " \"%s\" starting at "

                        + "index %d and ending at index %d.%n",

                        matcher.group(), matcher.start(), matcher.end());

                found = true;

            }

            if (!found) {

                console.format("No match found.%n");

            }

        }

    }

}


展开阅读全文