Count Occurrences of a Given Character using Regex in Java Last Updated : 15 Nov, 2021 Comments Improve Suggest changes Like Article Like Report Given a string and a character, the task is to make a function that counts the occurrence of the given character in the string using Regex. Examples: Input: str = "geeksforgeeks", c = 'e' Output: 4 'e' appears four times in str. Input: str = "abccdefgaa", c = 'a' Output: 3 'a' appears three times in str.Regular Expressions Regular Expressions or Regex is an API for defining String patterns that can be used for searching, manipulating, and editing a string in Java. Email validation and passwords are a few areas of strings where Regex is widely used to define the constraints. Regular Expressions are provided under java.util.regex package. Approach - Using Matcher.find() method in Java RegexGet the String in which it is to be matchedFind all occurrences of the given character using Matcher.find() function (in Java)For each found occurrence, increment the counter by 1 Below is the implementation of the above approach: Java // Java program to count occurrences // of a character using Regex import java.util.regex.*; class GFG { // Method that returns the count of the given // character in the string public static long count(String s, char ch) { // Use Matcher class of java.util.regex // to match the character Matcher matcher = Pattern.compile(String.valueOf(ch)) .matcher(s); int res = 0; // for every presence of character ch // increment the counter res by 1 while (matcher.find()) { res++; } return res; } // Driver method public static void main(String args[]) { String str = "geeksforgeeks"; char c = 'e'; System.out.println("The occurrence of " + c + " in " + str + " is " + count(str, c)); } } OutputThe occurrence of e in geeksforgeeks is 4 Related Article: Program to count the occurrence of a given character in a stringCount occurrence of a given character in a string using Stream API in Java Comment More infoAdvertise with us Next Article Count Occurrences of a Given Character using Regex in Java C code_r Follow Improve Article Tags : Java Java Programs java-regular-expression Java-String-Programs Practice Tags : Java Similar Reads Java program to count the occurrence of each character in a string using Hashmap Given a string, the task is to write a program in Java which prints the number of occurrences of each character in a string. Examples: Input: str = "GeeksForGeeks" Output: r 1 s 2 e 4 F 1 G 2 k 2 o 1 Input: str = "Ajit" Output: A 1 t 1 i 1 j 1 An approach using frequency[] array has already been dis 2 min read Java Program to Count the Occurrences of Each Character In Java, counting the occurrences of each character in a string is a fundamental operation that can be done in different ways. This process involves identifying the frequency of each character in the input string and displaying it in a readable format.Example: Input/output to count the occurrences o 5 min read Java program to count the characters in each word in a given sentence Write a Java program to count the characters in each word in a given sentence? Examples: Input : geeks for geeksOutput :geeks->5for->3geeks->5 Recommended: Please solve it on PRACTICE first, before moving on to the solution. Approach:Here we have to find out number of words in a sentence an 3 min read Count a Group of Words in a String Using Regex in Java Regular Expression is a powerful approach in Java for searching, Manipulating, and matching patterns with specific pattern requirements. In this article, we will learn to count a group of words in a string using regex. First I explain count a group of words in a string using regex as per requirement 4 min read Java Program to Find Occurrence of a Word Using Regex Java's regular expressions, or regex, let you do advanced text manipulation and matching. Regex offers a handy approach for searching for a term in a text wherever it appears. In this article, we will learn to find every occurrence of a word using regex. Program to Find Occurrence of a Word Using Re 2 min read Like