By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,665 Members | 2,225 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,665 IT Pros & Developers. It's quick & easy.

I am having trouble with finding a good regular expression

P: n/a
I am trying to find a regular expression that returns true in the
following cases but no others.

2.0
2.4
2.
324.0e345
234e34
34.e-43
234.673
-2.0
-2.4
-2.
-324.0e345
-234e34
-34.e-43
-234.673

In other words I am looking for a regular expression that returns true
only if there is a decimal in it. It can allow negative numbers. It can
have as many digits before and after the decimal. It can also allow
scientific notation with either the e or E. I have done some looking
around and found some but they returned false in some of the cases and
returned true for some of the cases I did not want them to. Please help.

Aug 17 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a

greenflame wrote:
I am trying to find a regular expression that returns true in the
following cases but no others.
[snip] 234e34 [snip] -234e34
The above two cases should return false. Why? Because of your comment
below:
In other words I am looking for a regular expression that returns true
only if there is a decimal in it.


This is the regular expression I've made. It appears to pass the test
cases (with the exception of above two), and the description of what
you're looking for.

var re = /^-?\d+\.\d*((e|E)-?\d+)?$/i;

If this expression does not work for you, could you post for which
values the expression does not pass? Then I can help you adjust
accordingly.

Hope this helps. :)

Aug 17 '05 #2

P: n/a
greenflame <al*********@yahoo.com> wrote in message news:11**********************@g43g2000cwa.googlegr oups.com...
I am trying to find a regular expression that returns true in the
following cases but no others.

2.0
2.4
2.
324.0e345
234e34
34.e-43
234.673
-2.0
-2.4
-2.
-324.0e345
-234e34
-34.e-43
-234.673

In other words I am looking for a regular expression that returns true
only if there is a decimal in it. It can allow negative numbers. It can
have as many digits before and after the decimal. It can also allow
scientific notation with either the e or E. I have done some looking
around and found some but they returned false in some of the cases and
returned true for some of the cases I did not want them to. Please help.


In addition to your specified formats (those with a decimal point), this expression will allow .2 and exponential
notation may include a '+' sign.

/^-?((\d+\.)|(\.\d))\d*(e(-|\+)?\d+)?$/i

--
S.C.

Aug 18 '05 #3

P: n/a
Ok. Thanks those look good. I will do some testing on them. Thanks.
This helps a lot.

Aug 18 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.