Thursday, October 1, 2015

Pattern finder and count how many times it is repeated

The program below finds a pattern and how many times it is repeated. This program returns a value "5" because there are 5 same patterns in the string data.


import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Finder1 {

 public static void main(String[] args) {
         String s = "adapeuflbdasabababababadsapjsoa";
         Pattern p = Pattern.compile("ab");
         Matcher m = p.matcher(s);
         int count = 0;
         while (m.find()) {
             count++;
         }
         System.out.println(count);
     }
}