

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Counting the number of groups Java regular expression
You can treat multiple characters as a single unit by capturing them as groups. You just need to place these characters inside a set of parentheses.
You can count the number of groups in the current match using the groupCount() method of the Matcher class. This method calculates the number of capturing groups in the current match and returns it.
Example
import java.util.regex.Matcher; import java.util.regex.Pattern; public class Test { public static void main(String[] args) { String str1 = "<p>This <b>is</b> an <b>example</b> HTML <b>script</b> where <b>ever</b> alternative <b>word</b> is <b>bold</b></p>."; //Regular expression to match contents of the bold tags String regex = "(t(\S+)t)(\s)"; String str = "the words tit tat tweet tostff tact that tilt text. start and end with the letter t "; //Creating a pattern object Pattern pattern = Pattern.compile(regex); //Matching the compiled pattern in the String Matcher matcher = pattern.matcher(str); while (matcher.find()) { System.out.println(matcher.group(0)); } System.out.println("Total capturing groups: "+matcher.groupCount()); } }
Output
tit tat tweet tact that tilt text tart Total capturing groups: 3
- Related Questions & Answers
- How to get the number of capture groups in Python regular expression?
- How do we use Python Regular Expression named groups?
- Non capturing groups Java regular expressions:
- Named captured groups Java regular expressions
- How do you access the matched groups in a JavaScript regular expression?
- Regular Expression Metacharacter in Java
- Regular Expression Metacharacter in Java
- Explain the concept of Regular expression.
- Working with simple groups in Java Regular Expressions
- Explain the Star Height of Regular Expression and Regular Language
- Regular Expression re+ Metacharacter in Java
- Regular Expression E Metacharacter in Java.
- Regular Expression Q Metacharacter in Java
- What is regular expression in Java?
- Regular Expression "(re)" Sub-Expression in Java
Advertisements