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.

`//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)); ` ` ` `} ` `} ` |

*chevron_right*

*filter_none*

Output:

2

**Related Articles :**

- Regular Expression Java
- Quantifiers
- 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 contribute@geeksforgeeks.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.

## Recommended Posts:

- Find all the patterns of "1(0+)1" in a given string | SET 1(General Approach)
- Check if a given string is a valid number (Integer or Floating Point) in Java | SET 2 (Regular Expression approach)
- Check if a binary string has a 0 between 1s or not | Set 2 (Regular Expression Approach)
- Find a string which matches all the patterns in the given array
- Check if a given string is a valid number (Integer or Floating Point) | SET 1(Basic approach)
- Extract maximum numeric value from a given string | Set 1 (General approach)
- Extract maximum numeric value from a given string | Set 2 (Regex approach)
- Suffix Tree Application 2 - Searching All Patterns
- Remove all occurrences of a character in a string | Recursive approach
- Check if a binary string has a 0 between 1s or not | Set 1 (General approach)
- Solve the Logical Expression given by string
- Find index of closing bracket for a given opening bracket in an expression
- Find the missing digit x from the given expression
- Given a string and an integer k, find the kth sub-string when all the sub-strings are sorted according to the given condition
- Evaluate a boolean expression represented as string
- Check if a string contains only alphabets in Java using Lambda expression
- Parsing String of symbols to Expression
- How to check string is alphanumeric or not using Regular Expression
- Minimum number of bracket reversals needed to make an expression balanced | Set - 2
- Check if expression contains redundant bracket or not | Set 2