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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T11:55:23+00:00 2026-05-24T11:55:23+00:00

I have an odd problem with a regular expression in Java. I tested my

  • 0

I have an odd problem with a regular expression in Java. I tested my Regex and my value here and it works. It says there are 3 groups (correct) the match for the first group (not group zero!) is SSS, the match for group 2 is BB and the match for group 3 is 0000. But my code below fails and I am quite at a loss why…

String pattern = "([^-]*)-([\\D]*)([\\d]*)";
String value = "SSS-BB0000";
Matcher matcher = Pattern.compile(pattern).matcher(value);
//group() is equivalent to group(0) - it fails to match though
matcher.group();

Here is a screenshot from the matching result of the above website:
enter image description here

I’d be really grateful if anyone could point out the mistake I am making… On an additional note: Strangely enough, if I execute the following code true is returned which implies a match should be possible…

//returns true
Pattern.matches(pattern, value);
  • 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-24T11:55:24+00:00Added an answer on May 24, 2026 at 11:55 am

    You need to call find() before group():

    String pattern = "([^-]*)-([\\D]*)([\\d]*)"; 
    String value = "SSS-BB0000";
    Matcher matcher = Pattern.compile(pattern).matcher(value); 
    if (matcher.find()) {
      System.out.println(matcher.group()); // SSS-BB0000
      System.out.println(matcher.group(0)); // SSS-BB0000
      System.out.println(matcher.group(1)); // SSS
      System.out.println(matcher.group(2)); // BB
      System.out.println(matcher.group(3)); // 0000
    }
    

    When you invoke matcher(value), you are merely creating a Matcher object that will be able to match your value. In order to actually scan the input, you need to use find() or lookingAt():

    References:

    • Matcher#find()
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have quite an odd problem here - I am using jquery for resizing
I have an odd problem...I'm using a documentation generator which generates a lot of
I have an odd problem in VS2008 running unit tests. When I run the
I have the odd problem that I am not able to open the properties
I have an odd problem with Django. I have a set of objects that
I have an odd problem where Hibernate is running more queries than I've asked
I recently encountered an odd problem with RSACryptoServiceProvider.VerifyHash . I have a web application
I am having a really odd problem with Flash CS4. I have a MovieClip
I have an odd problem. I have a unit test that keeps getting stuck
I have an odd problem. I have a page called search.aspx. When the search

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.