如何使用正则表达式检测浮点数 [英] How to detect a floating point number using a regular expression

查看:775
本文介绍了如何使用正则表达式检测浮点数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

处理浮点数的正则表达式(例如Java的Float)是什么?



答案必须与以下目标相匹配:

b
$ b

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

总之,它应该忽略前面的符号


  • / li>
  • 要求小数点左边的第一个字符不为零

  • 允许小数点两边有0个或更多个数字

  • 允许一个没有小数点的数字
  • 允许科学记数法
  • 允许大写或小写字母'e'

  • 允许正数或负数指数



对于那些想知道的人来说, 。我们把这个作为一个在我的研究生CS班上编译器的作业。我已经把我的答案转到了课堂上,并将其作为这个问题的答案发布。



[结语]
我的解决方案没有得到充分的信用,因为它没有处理小数点左边的1位以上的数字。这个任务确实提到了处理Java浮点数,尽管没有一个例子在小数点左边有超过1位的数字。

解决方案

[这是教授的回答]



定义:

N = [1-9]

D = 0 | N

E = [eE] [+ - ]? D +

L = 0 | (ND *)

然后浮点数可以与下面的值匹配:
$ b $(p。 。D +)E?)| (LE)


也可以使用D +而不是L,并且预先加上[+ - ]?。

一个常见的错误是写D *。 D *,但这只能匹配'。'。





有人询问有关前导符号;我应该问他为什么被排除,但从来没有机会。由于这是语法讲座的一部分,我的猜测是要么让问题变得简单(不太可能),要么在解析问题集的时候有一个小的细节,这样无论符号如何,浮点值都是焦点(可能)。



如果您通过表达式解析,例如


< - > -5.04e-10 + 3.14159E10



浮点值的符号是应用于该值的操作的一部分,而不是数字本身的属性。换句话说,


减去(5.04e-10)

add(3.14159E10)


来形成表达式的结果。虽然我确信数学家可能会争论这个观点,但请记住,这是来自解析的讲座。

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.

这篇关于如何使用正则表达式检测浮点数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆