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 7218151
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T21:26:43+00:00 2026-05-28T21:26:43+00:00

What I am looking for is to mimic behavior of C# regular expression (that

  • 0

What I am looking for is to mimic behavior of C# regular expression (that I really like) in Java’s regular expression API in a most easy-to-use way.

Basically, C# allows you to catch closures of “nested” groups in regular expression by allowing you to access Captures attribute of a Group object instances inside Matches. Description of the feature is here: MSDN.

For example, look at the code below:

public static void main(String[] args) {
    Pattern pattern = Pattern.compile("(abc((([\\d]+)\\s?)+)def\\s?)+?");
    Matcher matcher = pattern.matcher("abc123def abc567 341 123 789def");

    while(matcher.find()) {
        System.out.println(matcher.group(3));
    }
}

Output in Java is:

123
789

So, as you can see Java can not see other captures except (last) 789. In C# you would be able to see 567, 341, 123 and 789 inside Captures attribute.

Unfortunately I see that in Java I have access only to one capture of the nested group ([\\d]+), I can’t see to find a way to catch the other captures of the nested group. The question is basically “am I missing something here?“.

I know that I can just use another regex matcher on the match string of a larger group. I wish though to have it all in a single big RegEx which is filled with comments and easy to test in a (pretty cool) tool “Regulator v2”. I also know that the upper example can be done without the nested group, but this is just a rough example based on a real-life log parser RegEx with >20 groups, just to explain what is the problem.

EDIT: I introduced entire Java example to avoid misinterpretation of the problem.

  • 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-05-28T21:26:44+00:00Added an answer on May 28, 2026 at 9:26 pm

    It is not possible to achieve this in java.
    Java Matcher class will return last match of subgroup within each match.
    That is:

    For regex: (\w(\d))+ on string a1b2c3

    the returned groups will be [“a1b2c3”, “c3”, “3”].

    If the regex is changed to (\w(\d)) then it will return matches:

    ["a1", "a1", "1"], ["b2", "b2", "2"], ["c3", "c3", "3"]

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm looking for a way to mimic php's json_encode's behavior from node js. Here's
I am looking for the best way to mimic the facebook friend window, where
What I'm looking for is a way to mimic the MS-Access style continuous form
I'm looking to mimic the behavior of the activitypointer entity with custom entities. For
I was looking for ways to mimic something I've seen, however I'm really not
I'm looking to use in_place_editor_field on an Index page of items. Basically I want
Looking for an example that: Launches an EXE Waits for the EXE to finish.
Looking to do a very small, quick 'n dirty side project. I like the
Looking for a Linux application (or Firefox extension) that will allow me to scrape
Is it possible to mimic IQueryable with NHibernate? I was looking at Nhibernate docs

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.