
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
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 Articles
- 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 "(re)" Sub-Expression in Java
- Regular Expression "[^...]" construct in Java
- Regular Expression Metacharacter in Java
- Regular Expression Metacharacter in Java
- Regular expression “[X?+] ” Metacharacter Java
- Explain the Java regular expression construct "re?".
- Working with simple groups in Java Regular Expressions
- Regular Expression "^" (caret) Metacharacter in Java
- Regular Expression "$" (dollar) Metacharacter in Java
- Regular Expression "." (dot) Metacharacter in Java

Advertisements