java 正则 浮点数,如何使用正则表达式检测浮点数

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.

解决方案

[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
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值