Advertisement
blackhatMS

regex1.java

Oct 4th, 2014
236
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.06 KB | None | 0 0
  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.         while (true) {
  14.  
  15.             Pattern pattern =
  16.             Pattern.compile(console.readLine("%nEnter your regex: "));
  17.  
  18.             Matcher matcher =
  19.             pattern.matcher(console.readLine("Enter input string to search: "));
  20.  
  21.             boolean found = false;
  22.             while (matcher.find()) {
  23.                 console.format("I found the text" +
  24.                     " \"%s\" starting at " +
  25.                     "index %d and ending at index %d.%n",
  26.                     matcher.group(),
  27.                     matcher.start(),
  28.                     matcher.end());
  29.                 found = true;
  30.             }
  31.             if(!found){
  32.                 console.format("No match found.%n");
  33.             }
  34.         }
  35.     }
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement