简体   繁体   English

TCL的TCL regexp浮点数失败

[英]TCL regexp for float fails at single digit

I have developed the following regexp to capture float numbers. 我开发了以下正则表达式来捕获浮点数。

([+-]?[0-9]+\.?[0-9]+([eE][-+]?[0-9]+)?)

It works fine for such things as 4.08955e-11 or 3.57 . 它适用于4.08955e-113.57类的东西。 Now by stupid chance my parser came across 0 and failed. 现在,我的解析器偶然遇到0 ,但失败了。 I guess I need to make all following the decimal point optional. 我想我需要将所有在小数点后的内容都设为可选。 But how do I do that? 但是我该怎么做呢?

Contrary to what one might think, matching every possible form of floating point number (including NaN etc) with a manageable regular expression that still discards eg impossibly large numbers or pseudo-octals is non-trivial. 与人们可能想到的相反,将所有可能的浮点数形式(包括NaN等)与可管理的正则表达式进行匹配,而该正则表达式仍会丢弃例如不可能的大数或伪八进制数,这并非易事。

There are some ideas about reducing the risk of false positives by using word boundaries, but note that those match boundaries between word characters (usually alphanumerics and underscore). 有一些想法可以通过使用单词边界来降低误报的风险,但请注意,这些匹配单词字符之间的边界(通常是字母数字和下划线)。

The scan command allows simple and reliable validation and extraction of floating point numbers: scan命令允许简单可靠地验证和提取浮点数:

scan $number %f

也许使用替代方法:

{[-+]?(?:\y[0-9]+(?:\.[0-9]*)?|\.[0-9]+\y)(?:[eE][-+]?[0-9]+\y)?}

If you make all following the decimal point optional (which itself is optional) you could match values like 2. 如果将小数点后的所有内容设为可选 (其本身是可选的),则可以匹配2.2.

Note that your regex does not match a single digit because you match 2 times one or more digits [0-9]+ 请注意,您的正则表达式不匹配一位数字,因为您匹配2倍一位或多位数字[0-9]+

If you only want to match float numbers or zero you could use an alternation and for example use word boundaries \\b : 如果只想匹配浮点数或零,则可以使用交替形式,例如使用单词边界\\b

\\b[-+]?(?:[0-9]+\\.[0-9]+(?:[eE][-+]?[0-9]+)?|0)\\b

Explanation 说明

  • [-+]? Match optional + or - 匹配可选的+或-
  • \\b Word boundary \\b字边界
  • (?: Non capturing group (?:非捕获组
    • [0-9]+\\.[0-9]+ match one or more digits dot and one or more digits [0-9]+\\.[0-9]+匹配一个或多个数字点和一个或多个数字
    • (?:[eE][-+]?[0-9]+)? Optional exponent part 可选指数部分
    • | Or 要么
    • 0 Match literally 0字面上匹配
  • ) Close non capturing group )关闭非捕获组
  • \\b Word boundary \\b字边界

To match a float value that does not start with a dot and could be one or more digits without a dot you cold use use: 要匹配不以点开头并且可以是一个或多个不带点的数字的浮点值,请冷使用:

^[-+]?[0-9]+(?:\\.[0-9]+)?(?:[eE][-+]?[0-9]+)?$

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM