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

  • Home
  • SEARCH
  • 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 8122621
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T05:53:28+00:00 2026-06-06T05:53:28+00:00

I was wondering if Java had an equivalent to C#’s named pattern matching. For

  • 0

I was wondering if Java had an equivalent to C#’s named pattern matching. For example, in C# I can do something like this:

var pattern = @";(?<foo>\d{6});(?<bar>\d{6});";
var regex = new Regex(pattern , RegexOptions.None);
var match = regex.Match(";123456;123456;");

var foo = match.Groups["foo"].Success ? match.Groups["foo"].Value : null;
var bar = match.Groups["bar"].Success ? match.Groups["bar"].Value : null;

This just seems like a clean way to grab groups. Can Java do something similar, or do I need to grab groups based on index position?

String foo = matcher.group(0);
  • 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-06T05:53:29+00:00Added an answer on June 6, 2026 at 5:53 am

    This is supported starting in Java 7. Your C# code can be translated to something like this:

    String pattern = ";(?<foo>\\d{6});(?<bar>\\d{6});";
    Pattern regex = Pattern.compile(pattern);
    Matcher matcher = regex.matcher(";123456;123456;");
    boolean success = matcher.find();
    
    String foo = success ? matcher.group("foo") : null;
    String bar = success ? matcher.group("bar") : null;
    

    You have to create a Matcher object which doesn’t actually perform the regex test until you call find().

    (I used find() because it can find a match anywhere in the input string, like the Regex.Match() method. The .matches() method only returns true if the regex matches the entire input string.)

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

Sidebar

Related Questions

I was wondering if scala had an equivalent to java's @SuppressWarnings that can be
I'm wondering about computational efficiency. I'm going to use Java in this example, but
Was wondering if anyone can point me to an open source java quant library
I was wondering if I can develop a Desktop App for LinkedIn using Java.
I was wondering if something exists (in Java world) able to take an snapshot
I was wondering, If i had a java class, that wanted to consult a
I recently read this question which had a solution about labeling loops in Java.
I'm wondering why Java has this strange behavior regarding a superclass and a subclass
I was wondering why in java constructors are not inherited? You know when you
Just wondering what the opposing framework in Java would be for creating modular GUI

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.