Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Java Arrays
  • Java Strings
  • Java OOPs
  • Java Collection
  • Java 8 Tutorial
  • Java Multithreading
  • Java Exception Handling
  • Java Programs
  • Java Project
  • Java Collections Interview
  • Java Interview Questions
  • Java MCQs
  • Spring
  • Spring MVC
  • Spring Boot
  • Hibernate
Open In App
Next Article:
Matcher group() method in Java with Examples
Next article icon

Matcher group(int) method in Java with Examples

Last Updated : 26 Nov, 2018
Comments
Improve
Suggest changes
Like Article
Like
Report
The group(int group) method of Matcher Class is used to get the group index of the match result already done, from the specified group. Syntax:
  public String group(int group)  
Parameters: This method takes a parameter group which is the group from which the group index of the matched pattern is required. Return Value: This method returns the index of the first character matched from the specified group. Exception: This method throws:
  • IllegalStateException if no match has yet been attempted, or if the previous match operation failed.
  • IndexOutOfBoundsException if there is no capturing group in the pattern with the given index.
Below examples illustrate the Matcher.group() method: Example 1: Java
// Java code to illustrate group() method  import java.util.regex.*;  public class GFG {     public static void main(String[] args)     {          // Get the regex to be checked         String regex = "(G*s)";          // Create a pattern from regex         Pattern pattern             = Pattern.compile(regex);          // Get the String to be matched         String stringToBeMatched             = "GeeksForGeeks";          // Create a matcher for the input String         Matcher matcher             = pattern                   .matcher(stringToBeMatched);          // Get the current matcher state         MatchResult result             = matcher.toMatchResult();         System.out.println("Current Matcher: "                            + result);          while (matcher.find()) {             // Get the group matched using group() method             System.out.println(matcher.group(1));         }     } } 
Output:
Current Matcher: java.util.regex.Matcher[pattern=(G*s) region=0,13 lastmatch=] s s
Example 2: Java
// Java code to illustrate group() method  import java.util.regex.*;  public class GFG {     public static void main(String[] args)     {          // Get the regex to be checked         String regex = "(G*G)";          // Create a pattern from regex         Pattern pattern             = Pattern.compile(regex);          // Get the String to be matched         String stringToBeMatched             = "GFGFGFGFGFGFGFGFGFG";          // Create a matcher for the input String         Matcher matcher             = pattern                   .matcher(stringToBeMatched);          // Get the current matcher state         MatchResult result             = matcher.toMatchResult();         System.out.println("Current Matcher: "                            + result);          while (matcher.find()) {             // Get the group matched using group() method             System.out.println(matcher.group(0));         }     } } 
Output:
Current Matcher: java.util.regex.Matcher[pattern=(G*G) region=0,19 lastmatch=] G G G G G G G G G G
Reference: https://docs.oracle.com/javase/9/docs/api/java/util/regex/Matcher.html#group-int-

Next Article
Matcher group() method in Java with Examples

K

Kirti_Mangal
Improve
Article Tags :
  • Java
  • Java - util package
  • Java-Functions
  • Java-Matcher
Practice Tags :
  • Java

Similar Reads

  • Matcher group() method in Java with Examples
    The group() method of Matcher Class is used to get the input subsequence matched by the previous match result. Syntax: public String group() Parameters: This method do not takes any parameter. Return Value: This method returns the String which is the input subsequence matched by the previous match.
    2 min read
  • MatchResult group(int) method in Java with Examples
    The group(int group) method of MatchResult Interface is used to get the group index of the match result already done, from the specified group. Syntax: public String group(int group) Parameters: This method takes a parameter group which is the group from which the group index of the matched pattern
    2 min read
  • Matcher groupCount() method in Java with Examples
    The groupCount() method of Matcher Class is used to get the number of capturing groups in this matcher's pattern. Syntax: public int groupCount() Parameters: This method do not takes any parameter. Return Value: This method returns the number of capturing groups in this matcher's pattern. Below exam
    2 min read
  • Matcher find(int) method in Java with Examples
    The find(int start) method of Matcher Class attempts to find the next subsequence after the specified subsequence number, passed as parameter, of the input sequence that find the pattern. It returns a boolean value showing the same. Syntax: public boolean find(int start) Parameters: This method take
    2 min read
  • Matcher find() method in Java with Examples
    The find() method of Matcher Class attempts to find the next subsequence of the input sequence that find the pattern. It returns a boolean value showing the same. Syntax: public boolean find() Parameters: This method do not takes any parameter. Return Value: This method returns a boolean value showi
    2 min read
  • MatchResult group() method in Java with Examples
    The group() method of MatchResult Interface is used to get the input subsequence matched by the previous match result.Syntax: public String group() Parameters: This method do not takes any parameter.Return Value: This method returns the String which is the input subsequence matched by the previous m
    2 min read
  • Matcher end(int) method in Java with Examples
    The end(int group) method of Matcher Class is used to get the offset after the end index of the match result already done, from the specified group. Syntax: public int end(int group) Parameters: This method takes a parameter group from which the offset after the end index of the matched pattern is r
    2 min read
  • Matcher group(String) method in Java with Examples
    The group(String string) method of Matcher Class is used to get the group of the match result already done, from the specified string. Syntax: public String group(String string) Parameters: This method takes a parameter string which is the String from which the group index of the matched pattern is
    2 min read
  • Matcher hitEnd() method in Java with Examples
    The hitEnd() method of Matcher Class is used to check if this the matching of the pattern on this matcher has stopped or not. The matching ends when no more matched group is found in the matcher. This method returns a boolean value stating the same. Syntax: public boolean hitEnd() Parameters: This m
    3 min read
  • MatchResult groupCount() method in Java with Examples
    The groupCount() method of MatchResult Interface is used to get the number of capturing groups in this matcher's pattern. This method returns an integer value which is the number of groups found while matching this matcher.Syntax: public int groupCount() Parameters: This method do not takes any para
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences