Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and the Yahoo Answers website is now in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

i need help with java regex!?

for form validation

i need to only accept input that java can interpret as a float since im saving the input into a float variable

such as:

123

123.456

and not:

123.

.

abcd

!@#$%&

thanks!

1 Answer

Relevance
  • ?
    Lv 7
    7 years ago
    Favorite Answer

    why not get the specification for a float and build it from that? it's complicated.

    you forgot for instance scientific format.

    you also forgot that floats can be

    optional signs followed by

    integer

    .frac

    integer.frac

    any combination of above followed by e followed by optional signs followed by decimal digits OR

    integer.fracP

    followed by P followed by optional sign followed by decimal digits.

    where integer and frac must be same kind of number base (like octal) and in the case of e, digits must be decimal digits (base 10).

    well, here it is. you will have to take the spaces out, YA truncates without them.

    ([+-]*(\d+|\.\d+ | \d+\.\d+) ([Ee][+-]*\d+)? | (0[xX][0-9A-Fa-f]+(\.[0-9A-Fa-f])? | 0[0-7]+(\.[0-7]+)? | [1-9]\d*(\.\d+)?)[pP]\d+)

    I think with p that it only allows and also requires 1 digit left of period(.). so it may be instead

    ([+-]*(\d+|\.\d+ | \d+\.\d+) ([Ee][+-]*\d+)? | (0[xX][0-9A-Fa-f]\.[0-9A-Fa-f] | 0[0-7]\.[0-7]+ | [1-9]\.\d+)[pP]\d+)

    and now there's a p that's a new part of the new IEEE 754 floating point representation I think. look under basic data types and literals under javadoc.

    http://docs.oracle.com/javase/8/docs/api/index.htm...

    if you are doing javascript form validation using pattern="", make sure you replace any \d with [0-9]

    http://jesusnjim.com/programming/regular-expressio...

Still have questions? Get your answers by asking now.