您的位置:首页 > 其它

正则表达式使用语句

2011-08-31 21:13 162 查看
package corejava.ioutils;

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");

}

}

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: