这个是以前找到的,忘了出处了!前几天有个同学问我怎么匹配含有030173这串数字的字符串,于是就把这个类找出来测试了一下所写的正则表达式!
代码如下:
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("Found \"%s\" starting index %d ending index %d.%n",
matcher.group(), matcher.start(), matcher.end());
found = true;
}
if (!found) {
System.out.printf("No match found.%n");
}
}
}
}
用法:运行,在控制台输入你要测试的正则表达式,然后再输入要匹配的字符串即可!简单实用
附图片:
![](https://img-my.csdn.net/uploads/201204/18/1334757882_5280.jpg)