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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T02:47:44+00:00 2026-06-12T02:47:44+00:00

I have the strings: String myString1 = 22223 2342 1 98 333 3 665

  • 0

I have the strings:

    String myString1 = "22223 2342 1 98 333 3 665"

    String myString2 = "22323 3 222 34 33 1 98"

I want to only extract the value that has 1 digit, then a space, then only two digits.

    private static final Pattern p3DigitsGap = Pattern.compile(".*\\b\\d\\s\\d{2}\\b.*");

In both of the above strings, I’m looking to extract “1 98” I cannot just split by whitespace either side in case the pattern is at the end of the string.

        Matcher matcher = p3DigitsGap.matcher(myString1);
        if (matcher.find()) {
            Log.i("Matcher found");
            while (matcher.find() == true) {
                Log.i("Matcher is true");
                String extract = matcher.group(1);
                Log.d("extract: " + extract);
            }
        }

The String extract is never populated and throws an out of bounds exception if I remove the while loop. I added in the while loop and matcher.find() doesn’t equal true.

I tried swapping my pattern to:

    Pattern p3DigitsGap = Pattern.compile("[^0-9a-z]\\d\\s\\d{2}[^0-9a-z]");

I thought that the use of the .*\\b may be causing the issue, but I get the same result.

Please can someone help me understand where I’m going wrong.

Thanks in advance.

ANSWER EDIT: I marked the answer by Reimeus correct, as it works by using groups with the above regex. Please also see the answer by Tim Pietzcker for an alternative solution that improves on my original regex.

  • 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-12T02:47:46+00:00Added an answer on June 12, 2026 at 2:47 am

    Better to use groups when matching, so you could use:

    Pattern p3DigitsGap = Pattern.compile(".*\\b(\\d\\s\\d{2})\\b.*");
    Matcher matcher = p3DigitsGap.matcher(myString1);
    if (matcher.find()) {
       Log.i("Matcher is true");
       String extract = matcher.group(1);
       Log.d("extract: " + extract);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a little question. I want to put some strings in an String[].
I have string of 9 letters. string myString = 123987898; I want to retrieve
I have a string value. NSString *myString = @Latitude:-31.9504140#Longitude:115.8606040; How can I retrieve the
I have two strings String s1=426F62; String s2=457665; The strings are in hex representation.
I have a string String mainString=///BUY/SELL///ORDERTIME///RT///QTY///BROKERAGE///NETRATE///AMOUNTRS///RATE///SCNM///; Now I have another strings String str1= RT;
Assuming I have the following strings: string str1 = Hello World!; string str2 =
I have strings like this: Car is blue String could also be like this:
I have an array of strings and an IQueryable (called MyTypeQbl). I want to
Greetings, I have file with the following strings: string.Format({0},{1}, Having \Two\ On The Same
Let's say have a string... String myString = my*big*string*needs*parsing; All I want is to

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.