当前位置:   article > 正文

正则表达式使用语句_java常用类实验报告让 scanner类的实例使用正则表达式: string reg

java常用类实验报告让 scanner类的实例使用正则表达式: string reg

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

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Cpp五条/article/detail/475646
推荐阅读
相关标签
  

闽ICP备14008679号