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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T02:54:20+00:00 2026-06-01T02:54:20+00:00

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

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

Output :

0[a]1
1[]1

why this gives me two outputs while there is a single characters as the matcher.

I noticed that for this pattern it gives an zero-length always at the end of the source string.
Eg : when source is “abab” it gives

0[a]1
1[]1
2[a]3
3[]3
4[]4
  • 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-01T02:54:21+00:00Added an answer on June 1, 2026 at 2:54 am

    The regex special character ? (question mark) means “match the preceding thing zero or one time”.

    Since you are matching in a while loop (while (matcher.find()) {...) it finds both matches of the expression – one occurrence of “a” (at position 0, the string “a”) and zero occurrences of “a” (at position 1, the empty string at the very end).

    So here’s what your code snippet is matching (start/end indices are denoted by X/Y):

    String: " a b a b "
             ├─┼─┼─┼─┤
    Index:   0 1 2 3 4
    Match:   ╰┬╯ ╰┬╯ ╰- the empty string 4/4 (zero occurrences of "a").
              ||  |╰- the empty string 3/3 (zero occurrences of "a").
              ||  ╰ the string "a" 2/3 (one occurrence of "a").
              |╰ the empty string 1/1 (zero occurrences of "a").
              ╰ the string "a" 0/1 (one occurrence of "a").
    

    It doesn’t match at positions 0/0 or 2/2 since the expression is greedy, which means it will try to consider the next character (at positions 0/1, 2/3) as long as it doesn’t invalidate the match, which it doesn’t so they are skipped. To illustrate, if you were to match the string "bbbb" against the pattern a? then you would get five empty strings, one for each empty string at the beginning, end, and between each character.

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

Sidebar

Related Questions

My code : Pattern pattern = Pattern.compile(a?); Matcher matcher = pattern.matcher(ababa); while(matcher.find()){ System.out.println(matcher.start()+[+matcher.group()+]+matcher.end()); }
I have a function that uses Pattern#compile and a Matcher to search a list
Pattern pattern = Pattern.compile(^[a-z]+$); String string = abc-def; assertTrue( pattern.matcher(string).matches() ); // obviously fails
I am using while(matcher.find()) to loop through and retrieve things from a file. I
I have a pattern @@{} and given a string I need to find out
dir(re.compile(pattern)) does not return pattern as one of the lists's elements. Namely it returns:
i want to find whether a particular pattern exists in my text file or
For some reason this piece of Java code is giving me overlapping matches: Pattern
Found this code that breaks out CSV fields if contains double-quotes But I don't
How do I ignore case when using pcre_compile and pcre_exec? pcre_exec( pcre_compile(pattern,0,&error,&erroroffset,0), 0, string,

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.