2020-04-20

Reg-Number

写在前面

  • 利用正则表达式,匹配所有 Number 字面量

实践过程

  • NumbericLiteral 大纲

  • NumericLiteral ::

    • DecimalLiteral

      • DecimalLiteral 大纲

        • DecimalLiteral 详情

        • DecimalIntegerLiteral . DecimalDigits(opt) ExponentPart(opt)

          • DecimalIntegerLiteral

            (0)

            ([1-9][0-9]*)

          • .

            .

          • DecimalDigits

            [0-9]*

          • ExponentPart

            (e|E)(\+|\-)?([0-9])*

          • 可以推导出

            /^((0)|([1-9][0-9]*))?.?([0-9]*)((e|E)?(\+|\-)?([0-9]*))?$/

        • . DecimalDigits ExponentPart(opt)

          • 可以推导出

            /^((0)|([1-9][0-9]*))?.?([0-9]*)(((e|E)?(\+|\-)?([0-9])*)*)$/

        • DecimalIntegerLiteral ExponentPart(opt)

          /^((0)|([1-9][0-9]*))?.?([0-9])((e|E)?(\+|\-)?([0-9]\))?$/

        • 最终

          /^((0)|([1-9][0-9]*))?.?([0-9]*)((e|E)?(\+|\-)?([0-9]*))?$/

    • BinaryIntegerLiteral

      • BinaryIntegerLiteral 大纲

        • BinaryIntegerLiteral 详情

          /^0(b|B)(0|1)+$/

    • OctalIntegerLiteral

      • OctalIntegerLiteral 大纲

        • OctalIntegerLiteral 详情

          /^0(O|o)[0-7]+$/

    • HexIntegerLiteral

      • HexIntegerLiteral 大纲

        • HexIntegerLiteral 详情

          /^0(x|X)([0-9a-fA-F])+$/

  • 简单总结

    /^(((0)|([1-9][0-9]*))?.?([0-9]*)((e|E)?(\+|\-)?([0-9]*))?)|(0(b|B)(0|1)+)|(0(O|o)[0-7]+)|(0(x|X)([0-9a-fA-F])+)$/

  • 简化

    /^((((0)|([1-9]\d*))?.?(\d*)((e|E)?(\+|\-)?(\d*))?)|(0(b|B)(0|1)+)|(0(O|o)[0-7]+)|(0(x|X)([0-9a-fA-F])+))$/

    1
    /^((((0)|([1-9]\d*))?.?(\d*)((e|E)?(\+|\-)?(\d*))?)|(0(b|B)(0|1)+)|(0(O|o)[0-7]+)|(0(x|X)([0-9a-fA-F])+))$/

结果测试

  • 十六进制数: 0x0233acdfACDF
    • 十六进制数
  • 八进制数:0o023657
    • 八进制数
  • 二进制数: 0b01010111
    • 二进制数
  • 浮点数:.2373736
    • 浮点数
  • IEEE 754:1.2e+32
    • IEEE 754
  • 特殊情况 .0
    • 特殊情况

写在后面

  • 祝大家多多发财