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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T04:45:50+00:00 2026-05-18T04:45:50+00:00

I execute the following code: public static void test() { Pattern p = Pattern.compile(BIP[0-9]{4}E);

  • 0

I execute the following code:

public static void test() {
 Pattern p = Pattern.compile("BIP[0-9]{4}E");

 Matcher m = p.matcher("BIP1111EgjgjgjhgjhgjgjgjgjhgjBIP1234EfghfhfghfghfghBIP5555E");
 System.out.println(m.matches());
 while(m.find()) {
  System.out.println(m.group());
 }

}

What i cannot explain is when the code is executed with System.out.println(m.matches()); the matches printed are: BIP1234E and BIP5555E.
but when System.out.println(m.matches()); is removed from code the matche BIP1111E is also printed.

Can someone please explain how that’s possible ? Thnx a lot for your help.

  • 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-18T04:45:51+00:00Added an answer on May 18, 2026 at 4:45 am

    Matcher in Java maintains an index of found groups in the given string.

    For example in the string provided in you example – BIP1111EgjgjgjhgjhgjgjgjgjhgjBIP1234EfghfhfghfghfghBIP5555E

    There are 3 groups matching the pattern

    BIP1111E
    BIP1234E
    BIP5555E

    When matcher is created it starts from index 0. When we iterate over the matcher using m.find(), every time it finds a pattern it marks the index position of the found pattern.

    For example the first gourp is at start of the string – that is it starts at 0 and goes till 7th (0 based index) character of the string. Next time we say find() it starts from 8th character to find next match of pattern.

    m.matches tries to match the whole string and it also manipulates the internal index.

    when you call m.matches() before iterating using m.find() the index is moved from the initial 0. so the first group of BIP1111E is skipped if you call m.matches()

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

Sidebar

Related Questions

I have following code public class TEST { public static void main(String arg[]){ try
I have a Java program with code: public class Test1 { public static void
I have the following code: class Program { static void Main(string[] args) { string
I'm using the following code to execute a query in Lucene.Net var collector =
I have the following code: $bind = new COM(LDAP://CN=GroupName,OU=Groups,OU=Division,DC=company,DC=local); When I execute it from
The following code sample is an implementation of the Strategy pattern copied from Wikipedia
given the following code: public class MainFrame extends JFrame{ public MainFrame() throws HeadlessException {
I am trying to execute the following code. The code tries to parallely download
The following code executes a simple insert command. If it is called 2,000 times
The following code executes properly when the data key has no data to send,

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.