Advertisement
Guest User

Untitled

a guest
Mar 29th, 2017
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.91 KB | None | 0 0
  1. public static void main(String[] args) throws ParseException {
  2. Pattern p = Pattern.compile("\d\d\d");
  3. Matcher m = p.matcher("a123b");
  4. System.out.println(m.find());
  5. System.out.println(m.matches());
  6.  
  7. p = Pattern.compile("^\d\d\d$");
  8. m = p.matcher("123");
  9. System.out.println(m.find());
  10. System.out.println(m.matches());
  11. }
  12.  
  13. /* output:
  14. true
  15. false
  16. true
  17. true
  18. */
  19.  
  20. final Matcher subMatcher = Pattern.compile("\d+").matcher("skrf35kesruytfkwu4ty7sdfs");
  21. System.out.println("Found: " + subMatcher.matches());
  22. System.out.println("Found: " + subMatcher.find() + " - position " + subMatcher.start());
  23. System.out.println("Found: " + subMatcher.find() + " - position " + subMatcher.start());
  24. System.out.println("Found: " + subMatcher.find() + " - position " + subMatcher.start());
  25. System.out.println("Found: " + subMatcher.find());
  26. System.out.println("Found: " + subMatcher.find());
  27. System.out.println("Matched: " + subMatcher.matches());
  28.  
  29. System.out.println("-----------");
  30. final Matcher fullMatcher = Pattern.compile("^\w+$").matcher("skrf35kesruytfkwu4ty7sdfs");
  31. System.out.println("Found: " + fullMatcher.find() + " - position " + fullMatcher.start());
  32. System.out.println("Found: " + fullMatcher.find());
  33. System.out.println("Found: " + fullMatcher.find());
  34. System.out.println("Matched: " + fullMatcher.matches());
  35. System.out.println("Matched: " + fullMatcher.matches());
  36. System.out.println("Matched: " + fullMatcher.matches());
  37. System.out.println("Matched: " + fullMatcher.matches());
  38.  
  39. find()
  40.  
  41. public static void main(String[] args) {
  42. Pattern p = Pattern.compile("\d");
  43. String candidate = "Java123";
  44. Matcher m = p.matcher(candidate);
  45.  
  46. if (m != null){
  47. System.out.println(m.find());//true
  48. System.out.println(m.matches());//false
  49. }
  50. }
  51.  
  52. 1:Pattern.compile("[a-z]");
  53.  
  54. 2:Pattern.matcher("0a1b1c3d4");
  55.  
  56. 3:int count = 0;
  57.  
  58. 4:while(matcher.find()){
  59.  
  60. 5:count++: }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement