Find all the patterns of "1(0+)1" in a given string using Regular Expression Last Updated : 01 Mar, 2023 Summarize Comments Improve Suggest changes Share Like Article Like Report In Set 1, we have discussed general approach for counting the patterns of the form 1(0+)1 where (0+) represents any non-empty consecutive sequence of 0’s.In this post, we will discuss regular expression approach to count the same. Examples: Input : 1101001 Output : 2 Input : 100001abc101 Output : 2 Below is one of the regular expression for above pattern 10+1 Hence, whenever we found a match, we increase counter for counting the pattern.As last character of a match will always '1', we have to again start searching from that index. Implementation: C++ #include <iostream> #include <regex> class GFG { public: static int patternCount(std::string str) { // regular expression for the pattern std::regex regex("10+1"); // compiling regex std::smatch match; // counter int counter = 0; // whenever match found // increment counter while (std::regex_search(str, match, regex)) { // As last character of current match // is always one, starting match from that index str = match.suffix().str(); counter++; } return counter; } }; // Driver Method int main() { std::string str = "1001ab010abc01001"; std::cout << GFG::patternCount(str) << std::endl; return 0; } Java //Java program to count the patterns // of the form 1(0+)1 using Regex import java.util.regex.Matcher; import java.util.regex.Pattern; class GFG { static int patternCount(String str) { // regular expression for the pattern String regex = "10+1"; // compiling regex Pattern p = Pattern.compile(regex); // Matcher object Matcher m = p.matcher(str); // counter int counter = 0; // whenever match found // increment counter while(m.find()) { // As last character of current match // is always one, starting match from that index m.region(m.end()-1, str.length()); counter++; } return counter; } // Driver Method public static void main (String[] args) { String str = "1001ab010abc01001"; System.out.println(patternCount(str)); } } Python3 # Python program to count the patterns # of the form 1(0+)1 using Regex import re def patternCount(str): # regular expression for the pattern regex = "10+1" # compiling regex p = re.compile(regex) # counter counter=0 # whenever match found # increment counter for m in re.finditer(p,str): counter+=1 return counter # Driver Method str = "1001ab010abc01001" print(patternCount(str)) # This code is contributed by Pushpesh Raj C# // C# program to count the patterns // of the form 1(0+)1 using Regex using System; using System.Text.RegularExpressions; class GFG { static int patternCount(String str) { // regular expression for the pattern String regex = "10+1"; // compiling regex Regex p = new Regex(regex); // Matcher object Match m = p.Match(str); // counter int counter = 0; // whenever match found // increment counter while(m.Success) { // As last character of current match // is always one, starting match from that index m = m.NextMatch(); counter++; } return counter; } // Driver Method public static void Main (string[] args) { string str = "1001ab010abc01001"; Console.WriteLine(patternCount(str)); } } // This code is contributed by Aman Kumar JavaScript // Javascript program for the above approach function patternCount(str) { // regular expression for the pattern const regex = /10+1/g; // counter let counter = 0; // whenever match found // increment counter let match; while ((match = regex.exec(str)) !== null) { counter++; } return counter; } // Driver Method const str = "1001ab010abc01001"; console.log(patternCount(str)); // This code is contributed by adityashatmfh Output2 Time Complexity: O(n) Auxiliary Space: O(1) Related Articles : Regular Expression JavaQuantifiersExtracting each word from a String using RegexCheck if a given string is a valid number (Integer or Floating Point)Print first letter of each word in a string using regex Comment More infoAdvertise with us Next Article Count of occurrences of a "1(0+)1" pattern in a string K kartik Improve Article Tags : Strings Pattern Searching DSA java-regular-expression Practice Tags : Pattern SearchingStrings Similar Reads Find all the patterns of â1(0+)1â in a given string using Python Regex A string contains patterns of the form 1(0+)1 where (0+) represents any non-empty consecutive sequence of 0âs. Count all such patterns. The patterns are allowed to overlap. Note : It contains digits and lowercase characters only. The string is not necessarily a binary. 100201 is not a valid pattern. 2 min read Find all the patterns of "1(0+)1" in a given string (General Approach) A string contains patterns of the form 1(0+)1 where (0+) represents any non-empty consecutive sequence of 0's. Count all such patterns. The patterns are allowed to overlap. Note : It contains digits and lowercase characters only. The string is not necessarily a binary. 100201 is not a valid pattern. 5 min read Extracting all present dates in any given String using Regular Expressions Given a string Str, the task is to extract all the present dates from the string. Dates can be in the format i.e., mentioned below: DD-MM-YYYYYYYY-MM-DDDD Month YYYY Examples: Input: Str = "The First Version was released on 12-07-2008.The next Release will come on 12 July 2009. The due date for paym 6 min read Find all substrings with even 1s whose reverse is also present in given String Given a binary string str. The task is to find the size of the set(contains unique substrings) of substrings such that if there is a substring(suppose A) of length n with an even number of 1's and also there is another substring (suppose B) of the same length n and even number of 1's and it is rever 7 min read Count of occurrences of a "1(0+)1" pattern in a string Given an alphanumeric string, find the number of times a pattern 1(0+)1 occurs in the given string. Here, (0+) signifies the presence of non empty sequence of consecutive 0's. Examples: Input : 1001010001 Output : 3 First sequence is in between 0th and 3rd index. Second sequence is in between 3rd an 7 min read Count of substrings that start and end with 1 in given Binary String Given a binary string s, the task is to count all substrings that start and end with the character '1'. A valid substring must have both its first and last characters as '1', and can include one or more number of characters in between.Examples:Input: s = "00100101"Output: 3Explanation: Valid substri 7 min read Like