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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T23:46:47+00:00 2026-06-17T23:46:47+00:00

I have to perform partial pattern matching, so I tested pattern matching against the

  • 0

I have to perform partial pattern matching, so I tested pattern matching against the following input

Pattern p = Pattern.compile("hello");
Matcher m = p.matcher("[a-z]");

Can anybody explains me why

System.out.println(m.find() || m.hitEnd());

prints true while

System.out.println(m.hitEnd());

prints false?

  • 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-17T23:46:49+00:00Added an answer on June 17, 2026 at 11:46 pm

    Look at this program:

    Pattern p = Pattern.compile("hello");
    Matcher m = p.matcher("[a-z]");
    System.out.println(m.hitEnd()); // prints false
    System.out.println(m.find());  // prints false
    System.out.println(m.hitEnd()); // prints true
    

    Notice, the first call of m.hitEnd() returns false. Look at JavaDoc, it says:

    Returns true if the end of input was hit by the search engine in the
    last match operation performed by this matcher
    .

    Here it returns false, because it is called before the call of m.find(), so the matcher hasn’t performed any match operations, yet. After the call of m.find() it returns true (because find() consumes the complete input string and hits the end). The meaning of that is also explained in JavaDoc:

    When this method returns true, then it is possible that more input
    would have changed the result of the last search.

    When this returns true, it means the matcher hit the end of the input. In this case, hit means reached, not matched. (The input was completely consumed by the matcher).

    EDIT

    I hope it is wanted by you, that [a-z] is the input string for your regular expression hello, and it’s not the other way around. If you had

    Pattern p = Pattern.compile("[a-z]"); // The regex needs to be compiled.
    Matcher m = p.matcher("hello");       // The input is given to the matcher
    while (m.find()) {                    // In this case, returns true 5 times
        System.out.println(m.group() + ", ");
    }
    

    your output would be

    h, e, l, l, o, 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have to perform a select on a table in the following manner: I
I'd like to perform dynamic querying + partial serialization. Let say I have a
I have to perform async execution, and i have wrote the below code var
I have to perform mapping between ISO 3166-1 alpha-3 and IOC country codes. It
I have to perform a large number of replacements in some documents, and the
I have to perform a SQL query to get the top 10 albums according
I have to perform a query similar to: <?php //....connect to database $old =
I will have to perform a spelling check-like operation in Python as follows: I
I found that I have to perform a swap in python and I write
I have a requirement. Given say Test1,Test2, I have to perform a like operation.

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.