Advertisement
Guest User

Untitled

a guest
Dec 10th, 2018
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.30 KB | None | 0 0
  1. import java.util.regex.Pattern;
  2. import java.util.regex.Matcher;
  3. import java.io.*;
  4.  
  5. public class Regex {
  6. public static void main(String[] args) throws IOException{
  7. String strPattern, strInput;
  8. BufferedReader cin = new BufferedReader(new InputStreamReader(System.in));
  9.  
  10. while (true) {
  11. System.out.print("\nEnter your pattern: ");
  12. strPattern = cin.readLine();
  13. Pattern pattern = Pattern.compile(strPattern);
  14.  
  15. do {
  16. System.out.print("\nEnter your input string (0 to enter new pattern): ");
  17. strInput = cin.readLine();
  18.  
  19. if (!strInput.equals("0")) {
  20.  
  21. Matcher matcher = pattern.matcher(strInput);
  22.  
  23. boolean found = false;
  24. while (matcher.find()) {
  25. System.out.format("I found the text \"%s\" starting at " +
  26. "index %d and ending at index %d.\n",
  27. matcher.group(), matcher.start(), matcher.end());
  28. found = true;
  29. }
  30. if (!found) System.out.println("No match found.");
  31. } // end check if user wants to quit
  32.  
  33. } while (!strInput.equals("0"));
  34. } // infinite loop
  35. } // end main
  36. } // end class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement