Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package zad1;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
- public class Main {
- public static void main(String[] args) {
- 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.";
- System.out.println("1: "+policz("(\\w+)", tekst));
- System.out.println("2: "+policz(" ([1-9]|[12][0-9]|3[01]) ([a-z]+) \\d{4}", tekst));
- System.out.println("3: "+policz("(od \\d{4})", tekst));
- System.out.println("4: "+policz("([A-Z]\\w+)", tekst));
- System.out.println("5: "+policz("(\\.)", tekst));
- }
- public static int policz(String pattern, String tekst)
- {
- int a = 0;
- Pattern regex = Pattern.compile(pattern);
- Matcher matcher = regex.matcher(tekst);
- while (matcher.find())
- a++;
- return a;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement