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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T19:31:38+00:00 2026-05-23T19:31:38+00:00

I want to be able to parse below strings with a single regex using

  • 0

I want to be able to parse below strings with a single regex using groups? There may or may not be single/multiple whitespace between dash and numbers.

Input string examples:

"0.4 - 1.2 Mathematics"
"0.7-1.3 Physics"
"0.3-    0.7      Chemistry"
"4.5 Biology"
"2 Calculus"

group(1) -> lowGrade -> Float
group(2) -> highGrade -> Float (if exists)
group(3) -> class -> String

Can you help with the regex?
Thanks

  • 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-23T19:31:39+00:00Added an answer on May 23, 2026 at 7:31 pm

    So here is your working solution, if the “highGrade” is not available the second group is NULL.

    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    
    public class Main
    {
        public static void main(String[] args)
        {
            String Text = "0.4 - 1.2 Mathematics";
            Pattern p = Pattern.compile("^" + // Match the start of the string
                    "(\\d+(?:\\.\\d+)?)" + // Match the first float, the fraction is optional, (?:) is a non capturing group
                    "(?:\\s*-\\s*" + // Match the whitespace and the - . This part including the following float is optional
                    "(\\d+(?:\\.\\d+)?))?" + // Match the second float, because of the ? at the end this part is optional
                "\\s*(.*)" + // Match the whitespace after the numbers and put the rest of the chars in the last capturing group
                "$"); // Match the end of the string
    
            Matcher m = p.matcher(Text);
    
            if (m.matches()) {
                System.out.println(m.group(1));
                System.out.println(m.group(2));
                System.out.println(m.group(3));
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to be able to parse the following times using ParseExact() function: 01:02:03
I want to parse the below given data using resultsRequestSOAP object in order to
I want to be able to parse file paths like this one: /var/www/index.(htm|html|php|shtml) into
Background I want to be able to parse Javascript source in a Delphi Application.
I am able to parse the HTML but I want to extract the warning
I'm using LaTeX and BibTeX for an article, and I want to able to
So I want to be able to parse, and evaluate, dice expressions in C#.
I want to be able to parse a Time from a string in Ruby
Basically I want to be able to do be able to parse a JavaScript-like
Want to be able to provide a search interface for a collection of objects

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.