java 怎么利用正则表达式从给定的字符串中取出匹配规则字符串

如题所述

原理我不清楚,不过你改造一哈,应该就可以加以利用了!

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

}

}

}

}


温馨提示:内容为网友见解,仅供参考
无其他回答
相似回答