[Java] Java正则表达式 →→→→→进入此内容的聊天室

来自 , 2019-07-11, 写在 Java, 查看 145 次.
URL http://www.code666.cn/view/9adeb82f
  1. import java.io.Console;
  2. import java.util.regex.Pattern;
  3. import java.util.regex.Matcher;
  4.  
  5. public class RegexTestHarness {
  6.  
  7.     public static void main(String[] args) {
  8.         Console console = System.console();
  9.         if (console == null) {
  10.             System.err.println("No console.");
  11.             System.exit(1);
  12.         }
  13.      
  14.         while (true) {
  15.             Pattern pattern = Pattern.compile(console.readLine("%nEnter your regex: "));
  16.             Matcher matcher = pattern.matcher(console.readLine("Enter input string to search: "));
  17.             boolean found = false;
  18.             while (matcher.find()) {
  19.                 console.format("I found the text \"%s\" starting at index %d " +
  20.                         "and ending at index %d.%n",
  21.                         matcher.group(), matcher.start(), matcher.end());
  22.                 found = true;
  23.             }
  24.             if (!found) {
  25.                 console.format("No match found.%n");
  26.             }
  27.         }
  28.     }
  29. }//源代码片段来自云代码http://yuncode.net
  30.                        

回复 "Java正则表达式"

这儿你可以回复上面这条便签

captcha