RegEx将带有小数的理性文字与不完整的理性文字进行匹配

时间:2016-05-03 20:48:41

标签: javascript regex

要匹配像-3+-1.5/45,32这样的数字,我正在尝试编写一个正则表达式,而且我非常接近我所需要的。

我有以下(JavaScript)正则表达式来匹配某种格式的十进制数:

(   // 1st alternative:
(   [+-]?   // an optional unary +-
    [\d_]*  // 0-9 and _ digit separator any number of times
    [,.]?   // one of these decimal separators, once
    [\d]+   // 0-9 at least once
)
|   // 2nd alternative
(   [+-]?   // an optional unary +-
    [\d_]+  // 0-9 and _ digit separator, at least once
    [,.]?   // one of these decimal separators, once
    [\d]*   // 0-9 any number of times
)
)

也就是说,之前的匹配10_00_0000.901_00,990.99,.9不是 {{1 }或.

接下来,我尝试使用这个正则表达式来匹配使用上面提到的正则表达式的有理数表达式。

,

它的工作原理如下:

([+-]?[\d_]*[+-]?(([+-]?[\d_]*[,.]?[\d]+)|([+-]?[\d_]+[,.]?[\d]*))\/(([+-]?[\d_]*[,.]?[\d]*)|([+-]?[\d_]+[,.]?[\d]*)))

你可以在这里找到正则表达式和几个测试(单击左侧的“单元测试”,然后单击播放按钮):https://regex101.com/r/bB8eO2/5

所以你不必点击,这是我的测试:

(
[+-]?  // optional unary +-
[\d_]* // optional 0-9 and _ digit separator (for compound numbers)
[+-]?  // optional infix +- for 2+3/2 OR 2-2/3
(   // begin the regex from above, for before the / numer / denom separator
    (       // all as above
    [+-]?   // this is intentional: the numerator itself may be signed
    [\d_]*
    [,.]?
    [\d]+
    )
    |
    (
    [+-]?
    [\d_]+
    [,.]?
    [\d]*
    )
)
\/         // numerator / denominator separator
(([+-]?[\d_]*[,.]?[\d]*)|([+-]?[\d_]+[,.]?[\d]*))) // same as above

除了最后一次失败的测试外,一切正常。我已经摆弄了分母中的量词,我可以让最后一个测试通过,但是后面跟踪小数分隔符/5 assert that regex does not match PASS 4/5 assert that capture group 1 equals 4/5 PASS -1/3 assert that capture group 1 equals -1/3 PASS 3/-8 assert that capture group 1 equals 3/-8 PASS +4.12/-.90 assert that capture group 1 equals +4.12/-.90 PASS +1-6/7. assert that capture group 1 equals +1-6/7. PASS 1-+6/7 assert that capture group 1 equals 1-+6/7 PASS -1+-.4/5. assert capture group 1 equals -1+-.4/5. PASS -1+-.4/ assert regex does not match FAIL ,而倒数第二个和第一个测试失败:{{3} }

这是一个词法分析器,所以使用正则表达式而不是一个正确的解析器来测试它会很好:这是一个数字,还是一个标识符?

1 个答案:

答案 0 :(得分:1)

您可以使用

([+-]?[\d_]*[+-]?(?:[+-]?(?:[\d_]+(?:[,.]\d*)?|[.,]\d+))\/(?:[+-]?(?:[\d_]+(?:‌​[,.]\d*)?|[.,]\d+)))

请参阅this regex demo

模式分解:

(
 [+-]?[\d_]*[+-]?   # Optional + or - followed with 0+ digits or underscore and again optional - or +
 (?:[+-]?           # optional - or +
  (?:[\d_]+         # 1+ digits or underscore
   (?:[,.]\d*)?     # Optional sequence of a . or , followed with 0+ digits
   |                # or
   [.,]\d+          # , or , followed with 1+ digits
  )
 )
 \/
 (?:[+-]?           # optional - or +
  (?:[\d_]+         # 1+ digits or underscore
   (?:‌​[,.]\d*)?     # Optional sequence of a . or , followed with 0+ digits
   |                # or
   [.,]\d+          # , or , followed with 1+ digits
  )
 )
)