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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T00:02:15+00:00 2026-06-13T00:02:15+00:00

TL;DR What are the design decisions behind Matcher ‘s API? Background Matcher has a

  • 0

TL;DR

What are the design decisions behind Matcher‘s API?

Background

Matcher has a behaviour that I didn’t expect and for which I can’t find a good reason. The API documentation says:

Once created, a matcher can be used to perform three different kinds of match operations:
[…]
Each of these methods returns a boolean indicating success or failure. More information about a successful match can be obtained by querying the state of the matcher.

What the API documentation further says is:

The explicit state of a matcher is initially undefined; attempting to query any part of it before a successful match will cause an IllegalStateException to be thrown.

Example

String s = "foo=23,bar=42";
Pattern p = Pattern.compile("foo=(?<foo>[0-9]*),bar=(?<bar>[0-9]*)");
Matcher matcher = p.matcher(s);
System.out.println(matcher.group("foo")); // (1)
System.out.println(matcher.group("bar"));

This code throws a

java.lang.IllegalStateException: No match found

at (1). To get around this, it is necessary to call matches() or other methods that bring the Matcher into a state that allows group(). The following works:

String s = "foo=23,bar=42";
Pattern p = Pattern.compile("foo=(?<foo>[0-9]*),bar=(?<bar>[0-9]*)");
Matcher matcher = p.matcher(s);
matcher.matches(); // (2)
System.out.println(matcher.group("foo"));
System.out.println(matcher.group("bar"));

Adding the call to matches() at (2) sets the Matcher into the proper state to call group().

Question, probably not constructive

Why is this API designed like this? Why not automatically match when the Matcher is build with Patter.matcher(String)?

  • 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-13T00:02:16+00:00Added an answer on June 13, 2026 at 12:02 am

    Actually, you misunderstood the documentation. Take a 2nd look at the statement you quoted: –

    attempting to query any part of it before a successful match will cause an
    IllegalStateException to be thrown.

    A matcher may throw IllegalStateException on accessing matcher.group() if no match was found.

    So, you need to use following test, to actually initiate the matching process: –

     - matcher.matches() //Or
     - matcher.find()
    

    The below code: –

    Matcher matcher = pattern.matcher();  
    

    Just creates a matcher instance. This will not actually match a string. Even if there was a successful match.
    So, you need to check the following condition, to check for successful matches: –

    if (matcher.matches()) {
        // Then use `matcher.group()`
    }
    

    And if the condition in the if returns false, that means nothing was matched. So, if you use matcher.group() without checking this condition, you will get IllegalStateException if the match was not found.


    Suppose, if Matcher was designed the way you are saying, then you would have to do a null check to check whether a match was found or not, to call matcher.group(), like this: –

    The way you think should have been done:-

    // Suppose this returned the matched string
    Matcher matcher = pattern.matcher(s);  
    
    // Need to check whether there was actually a match
    if (matcher != null) {  // Prints only the first match
    
        System.out.println(matcher.group());
    }
    

    But, what if, you want to print any further matches, since a pattern can be matched multiple times in a String, for that, there should be a way to tell the matcher to find the next match. But the null check would not be able to do that. For that you would have to move your matcher forward to match the next String. So, there are various methods defined in Matcher class to serve the purpose. The matcher.find() method matches the String till all the matches is found.

    There are other methods also, that match the string in a different way, that depends on you how you want to match. So its ultimately on Matcher class to do the matching against the string. Pattern class just creates a pattern to match against. If the Pattern.matcher() were to match the pattern, then there has to be some way to define various ways to match, as matching can be in different ways. So, there comes the need of Matcher class.

    So, the way it actually is: –

    Matcher matcher = pattern.matcher(s);
    
       // Finds all the matches until found by moving the `matcher` forward
    while(matcher.find()) {
        System.out.println(matcher.group());
    }
    

    So, if there are 4 matches found in the string, your first way, would print only the first one, while the 2nd way will print all the matches, by moving the matcher forward to match the next pattern.

    I Hope that makes it clear.

    The documentation of Matcher class describes the use of the three methods it provides, which says: –

    A matcher is created from a pattern by invoking the pattern’s matcher
    method. Once created, a matcher can be used to perform three different
    kinds of match operations:

    • The matches method attempts to match the entire input sequence
      against the pattern.

    • The lookingAt method attempts to match the input sequence, starting
      at the beginning, against the pattern.

    • The find method scans the input sequence looking for the next
      subsequence that matches the pattern.

    Unfortunately, I have not been able find any other official sources, saying explicitly Why and How of this issue.

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

Sidebar

Related Questions

I am mainly interested in the design decision behind this. background information: FileSystemInfo is
I work on an application that has a both a GUI (graphical) and API
How do you ensure that the project will be build with good design decisions
Design requirement: Show a list of items the user can pick from After having
Design wise and performance wise which approach is recommended for handling multiple Zeromq sockets
This is a more a philosophical question about Apple's design decisions than a question
What is the rationale behind the design decision to have separate namespaces for values
While iterating through a std::map or std::vector or any container which has iterator in
Due to legacy design decisions we are using SqLite over file shares. We have
In Python it's possible to create a procedure that has no explicit return. i.e.:

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.