Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.00 KB | None | 0 0
  1. package zad1;
  2.  
  3. import java.util.regex.Matcher;
  4. import java.util.regex.Pattern;
  5.  
  6. public class Main {
  7.  
  8. public static void main(String[] args) {
  9.  
  10. String tekst = "Jan III Sobieski herbu Janina 031 sie 1774 (ur. 17 sierpnia 1629 w Olesku, zm. 17 czerwca 1696 w Wilanowie) – krol Polski i wielki ksiaze litewski od 1674, hetman wielki koronny od 1668, hetman polny koronny od 1666, marszalek wielki koronny od 1665, chorazy wielki koronny od 1656.";
  11.  
  12. System.out.println("1: "+policz("(\\w+)", tekst));
  13. System.out.println("2: "+policz(" ([1-9]|[12][0-9]|3[01]) ([a-z]+) \\d{4}", tekst));
  14. System.out.println("3: "+policz("(od \\d{4})", tekst));
  15. System.out.println("4: "+policz("([A-Z]\\w+)", tekst));
  16. System.out.println("5: "+policz("(\\.)", tekst));
  17. }
  18.  
  19. public static int policz(String pattern, String tekst)
  20. {
  21. int a = 0;
  22. Pattern regex = Pattern.compile(pattern);
  23. Matcher matcher = regex.matcher(tekst);
  24. while (matcher.find())
  25. a++;
  26.  
  27. return a;
  28. }
  29.  
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement