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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T01:25:29+00:00 2026-06-15T01:25:29+00:00

I need help with Regex to extract numbers from a string that is missing

  • 0

I need help with Regex to extract numbers from a string that is missing decimals.

Input
2 4.0 6.0 7 8 4.60

Output
2 7 8

Edit: Another follow up question to this regarding Regex and Java. Lets say that I want to add stuff to what I found (what I really want is taking 2 4.0 7.5 and turn it into 2.0 4.0 7.5, meaning adding missing .0 to the ones without a decimal), how would I do that and still keep the same untouched things of my String?

  • 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-15T01:25:30+00:00Added an answer on June 15, 2026 at 1:25 am

    For 2nd question: –

    Here’s the solution for your 2nd requirement: –

    String str = "2 4.0 6.0 7.5 8 4.60";
    str = str.replaceAll("(?<=^|[ ])(\\d+)(?=$|[ ])", "$1.0");
    
    System.out.println(str);  // Prints 2.0 4.0 6.0 7.5 8.0 4.60
    

    For 1st question : –

    You can use this Regex: –

    (?<=^|[ ])(\\d+)(?=$|[ ])
    

    This will match any sequence of digits followed and preceded by a space. Also, it matches digits at the end of the strings with the use of ^ and $.

    Here’s the implementation : –

        String str = "2 4.0 6.0 7 8 4.60";
        Pattern pattern = Pattern.compile("(?<=^|[ ])(\\d+)(?=[$ ])");
    
        Matcher matcher = pattern.matcher(str);
    
        while (matcher.find()) {
            System.out.print(matcher.group(1) + " ");
        }
    

    Output : –

    2 7 8
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need help with a regex to extract mac addresses from a large file.
I need help on regex or preg_match because I am not that experienced yet
I need some help. I am looking for a regex that would match the
I lack experience with regex and I need some help. I need to extract
I need to extract information from an LDAP connection string like this one: ldap://uid=adminuser,dc=example,c=com:secret@ldap.example.com/dc=basePath,dc=example,c=com
I am trying to extract the word need from this string. ctl00_ctl00_ContentMainContainer_ContentColumn1__needDont_Panel1 I have
I have a complex string that represents database tables. And I need to extract
I need to extract the ID number from a string. The results are given
Looking for some help with this perl regex. I need to extract (3) items
I need to extract URPlus1_S2_3 from the string: Last one: http://abc.imp/Basic2#URPlus1_S2_3, using regular expression

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.