1 测试用具

优质
小牛编辑
132浏览
2023-12-01

这节给出了一个可重用的测试用具 RegexTestHarness.java,用于探究构建 API 所支持的正则表达式。使用

java RegexTestHarness

这个命令来运行,没有被接受的命令行参数。这个应用会不停地循环执行下去[3],提示用户输入正则表达式和字符串。虽然说使用这个测试用具是可选的,但你会发现它用于探究下文所讨论的测试用例将更为方便。

import java.io.Console; 
import java.util.regex.Pattern; 
import java.util.regex.Matcher; 
 
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()) { 
                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"); 
            } 
        } 
    } 
}

在继续下一节之前,确认开发环境支持必需的包,并保存和编译这段代码。

【译者注】

由于当前版本的 Java Tutorial
是基于 JDK 6.0 编写的,上述的测试用具由于使用到 JDK 6.0 中新增的类库(java.io.Console),所以该用具只能在
JDK 6.0 的环境中编译运行,由于 Console 访问操作系统平台上的控制台,因此这个测试用具只能在操作系统的字符控制台中运行,不能运行在
IDE 的控制台中。

正则表达式是 JDK 1.4 所增加的类库,为了兼容 JDK 1.4 和 JDK 5.0 的版本,重新改写了这个测试用具,让其能适用于不同的版本。

JDK 5.0 适用的测试用具(RegexTestHarnessV5.java,该用具可以在 IDE 中执行),建议 JDK 6.0 环境也采用该用具。

import java.util.Scanner; 
import java.util.regex.Matcher; 
import java.util.regex.Pattern; 
 
public class RegexTestHarnessV5 { 
 
    public static void main(String[] args) { 
        Scanner scanner = new Scanner(System.in); 
        while (true) { 
            System.out.printf("%nEnter your regex: "); 
            Pattern pattern = Pattern.compile(scanner.nextLine()); 
            System.out.printf("Enter input string to search: "); 
            Matcher matcher = pattern.matcher(scanner.nextLine()); 
            boolean found = false; 
            while (matcher.find()) { 
                System.out.printf( 
                        "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) { 
                System.out.printf("No match found.%n"); 
            } 
        } 
    } 
}

JDK 1.4 适用的测试用具(RegexTestHarnessV4.java):

import java.io.BufferedInputStream; 
import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStreamReader; 
import java.util.regex.Matcher; 
import java.util.regex.Pattern; 
 
public class RegexTestHarnessV4 { 
 
    public static void main(String[] args) throws IOException { 
        BufferedReader br = new BufferedReader( 
                new InputStreamReader(new BufferedInputStream(System.in)) 
            ); 
        while (true) { 
            System.out.print("\nEnter your regex: "); 
            Pattern pattern = Pattern.compile(br.readLine()); 
            System.out.print("Enter input string to search: "); 
            Matcher matcher = pattern.matcher(br.readLine()); 
            boolean found = false; 
            while (matcher.find()) { 
                System.out.println("I found the text \"" + matcher.group() + 
                        "\" starting at index " + matcher.start() + 
                        " and ending at index " + matcher.end() + 
                        "."); 
                found = true; 
            } 
            if (!found) { 
                System.out.println("No match found."); 
            } 
        } 
    } 
}