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.
Input : 1101001 Output : 2 Input : 100001abc101 Output : 2
Below is one of the regular expression for above pattern
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.
Related Articles :
- Regular Expression Java
- Extracting each word from a String using Regex
- Check if a given string is a valid number (Integer or Floating Point)
- Print first letter of each word in a string using regex
This article is contributed by Gaurav Miglani. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
In case you wish to attend live classes with industry experts, please refer Geeks Classes Live