Matching Specific String

  • + 0 comments

    Java 15:

    import java.io.*;
    import java.util.*;
    import java.util.regex.Pattern;
    import java.util.regex.Matcher;
    
    public class Solution {
    
        public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);
            Pattern pattern = Pattern.compile("hackerrank");
            int count = 0;
            while(sc.hasNext())
            {   Matcher matcher = pattern.matcher(sc.next());
                if (matcher.find()) count++;
            }
            System.out.println("Number of matches : "+count);
        }
    }