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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T02:35:07+00:00 2026-05-14T02:35:07+00:00

What is a good regular expression for handling a floating point number (i.e. like

  • 0

What is a good regular expression for handling a floating point number (i.e. like Java’s Float)

The answer must match against the following targets:

 1) 1.  
 2) .2   
 3) 3.14  
 4) 5e6  
 5) 5e-6  
 6) 5E+6  
 7) 7.e8  
 8) 9.0E-10  
 9) .11e12  

In summary, it should

  • ignore preceding signs
  • require the first character to the left of the decimal point to be non-zero
  • allow 0 or more digits on either side of the decimal point
  • permit a number without a decimal point
  • allow scientific notation
  • allow capital or lowercase ‘e’
  • allow positive or negative exponents

For those who are wondering, yes this is a homework problem. We received this as an assignment in my graduate CS class on compilers. I’ve already turned in my answer for the class and will post it as an answer to this question.

[Epilogue]
My solution didn’t get full credit because it didn’t handle more than 1 digit to the left of the decimal. The assignment did mention handling Java floats even though none of the examples had more than 1 digit to the left of the decimal. I’ll post the accepted answer in it’s own post.

  • 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-14T02:35:07+00:00Added an answer on May 14, 2026 at 2:35 am

    [This is the answer from the professor]

    Define:

    N = [1-9]
    D = 0 | N
    E = [eE] [+-]? D+
    L = 0 | ( N D* )

    Then floating point numbers can be matched with:

    ( ( L . D* | . D+ ) E? ) | ( L E )

    It was also acceptable to use D+ rather than L, and to prepend [+-]?.

    A common mistake was to write D* . D*, but this can match just ‘.’.

    [Edit]
    Someone asked about a leading sign; I should have asked him why it was excluded but never got the chance. Since this was part of the lecture on grammars, my guess is that either it made the problem easier (not likely) or there is a small detail in parsing where you divide the problem set such that the floating point value, regardless of sign, is the focus (possible).

    If you are parsing through an expression, e.g.

    -5.04e-10 + 3.14159E10

    the sign of the floating point value is part of the operation to be applied to the value and not an attribute of the number itself. In other words,

    subtract (5.04e-10)
    add (3.14159E10)

    to form the result of the expression. While I’m sure mathematicians may argue the point, remember this was from a lecture on parsing.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

solution:[a-zA-Z0-9.!#$%&'*+-/=?\^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)* is a good choice I am using a regular expression like the below
On the following two strings I'd like to create a regular expression that returns
Good day! My regular expression is really bad and I would like to ask
Possible Duplicate: Regular expression where part of string must be number between 0-100 I'd
can somebody point me to a good regular expression resource (for php if it
I am not very good with regular expression. I have a string like: var
I read in Javascript: The Good Parts by Douglas Crockford that javascript regular expression
I am not very good at regular expression but want to do some thing
I'm not that good with regular expressions... I need a JavaScript regular expression that
I'm no good at Regular Expressions, really! I would like to specifically detect WebKit

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.