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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T03:49:23+00:00 2026-06-14T03:49:23+00:00

The following Java code is intended to capture the word abc, but instead it

  • 0

The following Java code is intended to capture the word “abc”, but instead it gives “null”:

Pattern p = Pattern.compile("^.*(\\ba\\w*\\b)?.*$");
Matcher m = p.matcher("xxx abc yyy");
if (m.matches()) System.out.println(m.group(1));

If you remove the question mark, it correctly captures “abc”. The question mark is greedy, so I would have thought the original code should also give “abc”.

Thank you to anyone who can explain why!

  • 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-14T03:49:24+00:00Added an answer on June 14, 2026 at 3:49 am

    The .* at the beginning of your regex is greedy, so it will initially try to match as many characters as possible (the entire string). As the regex engine moves onto the capturing group, it sees that \ba\w*\b cannot match at the end of the string, but because the group is optional it will not backtrack and try to find a match.

    To fix this, just change the .* at the beginning to .*?, which will still match zero or more characters but it will try to match as few as possible (lazy instead of greedy):

    Pattern p = Pattern.compile("^.*?(\\ba\\w*\\b)?.*$");
    

    The other alternative would be to make your capturing group required by removing the ? after it. This would force the regex engine to backtrack until the group match is made. This probably isn’t what you want though, as it would change the meaning of the regex (fewer strings would be matched).

    edit: Looks like I really should have tested this! As it turns out just changing the .* to .*? doesn’t help here, because your group still can’t match at the beginning, and the entire string will be matched by the .* at the end (even if you change it to .*?).

    Your best bet here is to just remove the ? after the group so that the group is required. If you still want to match all strings but have the group be null for strings that don’t match your group, you could use the following regex:

    ^(?:.*(\ba\w*\b).*|.*)$
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

in the following java code a JButton is created but at the same time
The following Java code is a very simple piece of code, but what are
I'm trying to connect to a remote database using the following java code, but
I have following Java code: JFrame frame = new JFrame(); frame.setTitle(Sudoku); frame.setLocationRelativeTo(null); frame.setSize(500, 500);
The following Java code is throwing a compiler error: if ( checkGameTitle(currGame) ) ArrayList<String>
The following java code prints Hello World on my co-worker's computer: public void testJDBC()
I have the following java code: import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.Statement;
If I have the following Java code: int[][] readAPuzzle() { Scanner input = new
Consider the following Java code: volatile boolean v1 = false; volatile boolean v2 =
I have the following Java code: final Future future = exeService.submit( new Runnable() {

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.