Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9238531
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T07:47:06+00:00 2026-06-18T07:47:06+00:00

I have java code with matcher to find number of occurence in string using

  • 0

I have java code with matcher to find number of occurence in string using mattcher.find method.

following is my code

String text = "INCLUDES(ABC) EXCLUDES(ABC) EXCLUDES(ABC) INCLUDES(EFG) INCLUDES(IJK)";

String patternString = "INCLUDES(.)";

Pattern pattern = Pattern.compile(patternString);
Matcher matcher = pattern.matcher(text);

int count = 0;
while(matcher.find()) {
    count++;
    System.out.println("found: " + count + " : "
            + matcher.start() + " - " + matcher.end());
    System.out.println(" - " +text.substring(matcher.start(), matcher.end()));
}

which returns output as

found: 1 : 0 - 9
 - INCLUDES(
found: 2 : 42 - 51
 - INCLUDES(
found: 3 : 56 - 65
 - INCLUDES(

Instead of i want Regex to find and return number of occurrences as INCLUDES(*)

any solution is appriciated. expected output should be loop printing values

INCLUDES(ABC)
INCLUDES(EFG)
INCLUDES(IJK)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T07:47:07+00:00Added an answer on June 18, 2026 at 7:47 am

    Your regex is not correct. You are just capturing a single character inside the bracket, and hence your regex will fail.

    Try using this: –

    "\\w+\\(.*?\\)"
    

    And then get group(0), or just group(): –

    String text = "INCLUDES(ABC) EXCLUDES(ABC) EXCLUDES(ABC) INCLUDES(ABC) INCLUDES(ABC)";
    Matcher matcher = Pattern.compile("\\w+\\(.*?\\)").matcher(text);
    
    while (matcher.find()) {
        System.out.println(matcher.group());
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have following Java code: String s2 = SUM 12 32 42; Pattern pat1
I have the below groovy code import java.util.regex.Matcher; import java.util.regex.Pattern; String myInput=License_All (12313) String
I have the following (Java) code: public class TestBlah { private static final String
I have java source code in a text file. There has to be entered
I have a problem in applying a regex in my Java code. My text
I have the following Java code: long a = Long.parseLong(11001100, 2); long b =
I'm trying to extract two blocks of code from a text file using Java
Need some regex help in Java. I have a string, which contains text and
In the following Java code: public static void main(String[] args) { String largeText =
I have Java code and TeX documents in the same project. I use TeXlipse

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.