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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T04:18:49+00:00 2026-06-01T04:18:49+00:00

My code : Pattern pattern = Pattern.compile(a?); Matcher matcher = pattern.matcher(ababa); while(matcher.find()){ System.out.println(matcher.start()+[+matcher.group()+]+matcher.end()); }

  • 0

My code :

Pattern pattern = Pattern.compile("a?");
Matcher matcher = pattern.matcher("ababa");
while(matcher.find()){
   System.out.println(matcher.start()+"["+matcher.group()+"]"+matcher.end());
}

Output :

0[a]1
1[]1
2[a]3
3[]3
4[a]5
5[]5

What I know :

  • “a?” stands for zero or one occurrence of the character ‘a’.

Java API says :

  • matcher.start() returns the start index of the previous match.
  • matcher.end() returns the offset after the last character matched.
  • matcher.group() returns the input subsequence matched by the previous
    match. For a matcher m with input sequence s, the expressions
    m.group() and s.substring(m.start(), m.end()) are equivalent. And for
    some patterns, for example a*, match the empty string. This method
    will return the empty string when the pattern successfully matches
    the empty string in the input.

What I want to know:

  1. In which situations does the regex engine encounters a zero
    occurrence of a given character(s) – Here for character ‘a’.
  2. In those situation what are values actually returns by the start(),
    end() and group() methods in the matcher. I have mentioned what the
    java API said. But I’m little unclear when it comes to the practical
    situation as above.
  • 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-01T04:18:51+00:00Added an answer on June 1, 2026 at 4:18 am

    The ? is a greedy quantifier, therefore it will first try to match the 1-occurence before trying the 0-occurence. In you string,

    1. it starts with the first char ‘a’ and tries to match agains the 1-occurence. The ‘a’ char matches and so it returns the first result you see
    2. then it moves forward and find a ‘b’. The ‘b’ char does not match your regexp 1-occurence, so the engine backtracks and attempt to match a 0-occurence. Result is that the empty string is matched–> you get your second result.
    3. then it moves ahead of b since no more matches are possible there and it starts again with your second ‘a’ char.
    4. etc… you get the point…

    It is a bit more complicated than that but that is the main idea. When the 1-occurence cannot match, it will then try with the 0-occurence.

    As for the values of start, end and group, they will be where the match starts, ends and the group is what has been matched, so in the first 0-occurence match of your string, you get 1, 1 and the emtpy string. I am not sure this really answers your question.

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

Sidebar

Related Questions

Pattern p2 = Pattern.compile(.*); Matcher m2 = p2.matcher(true); System.out.println(m2.matches() + [ + m2.group() +
The following code blocks on my system. Why? System.out.println( Pattern.compile( ^((?:[^'\][^'\]*|\[^\]*\|'[^']*')*)/\\*.*?\\*/(.*)$, Pattern.MULTILINE | Pattern.DOTALL
i have the following code Pattern keyPattern = Pattern.compile(key); Matcher matcher = keyPattern.matcher(str); return
I have a below peace of code : Pattern p = Pattern.compile(^\\d|^\\W|^\\s); Matcher m
In this statement, taken from the Pagerank source code: Pattern.compile(\\[.+?\\]); What does the pattern
I often use this code pattern: while(true) { //do something if(<some condition>) { break;
The following code works: String str= test with foo hoo; Pattern pattern = Pattern.compile(foo);
The following code matches the two expressions and prints success. import java.util.regex.Matcher; import java.util.regex.Pattern;
Java Code: import java.util.regex.Matcher; import java.util.regex.Pattern; public class RegExpTest { public static void main(String[]
This is our code: import java.io.File; import java.io.FileNotFoundException; import java.util.*; import java.util.regex.Matcher; import java.util.regex.Pattern;

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.