Golang Regex:在正则表达式中匹配一个浮点数字的方法

1,038 阅读3分钟

概述

一个浮点数可以有以下属性

  • 它可以有一个负号和正号

  • 当小数部分存在时,整数部分可以是可选的

  • 如果整数部分存在,点和小数部分可以是可选的。

  • 它可以有一个指数,也可以没有

所以下面是有效的浮点数

1.2
.12
12
12.
+1.2
-1.2
1.2e3

以下是无效的浮点数

  • 一个空字符串

  • 只有+或-符号

  • 一个单点

  • 多个0的前缀,例如00.1或001

  • 任何类似+.或-的东西

  • 指数前的一个点。 例如:1.e2

  • 浮点数之前或之后的任何其他字符。Eg a1.3 或 a1.3b 或 1.3b

下面是无效的浮点数的例子

""
.
00.1
001
+
-
+.
-.
1.e2
a1.2
1.2b
a1.2b

让我们先看看一个简单的铰链,它只匹配整数、点、和小数部分。

^(?:(?:0|[1-9]\d*)(?:\.\d*)?|\.\d+)$

在高层次上,整个词条有两个部分,它们是OR的关系

  • (?:0|[1-9]\d*)(?:\.\d*)? - 这部分捕获的是整数部分总是存在的,小数部分是可选的

  • \.\d+ - 这捕获了整数部分不存在而小数部分总是存在的部分。

让我们来剖析这个词组

让我们通过让它接受负号或正号来使其更加复杂。注意,负号或正号是可选的

^[+\-]?(?:(?:0|[1-9]\d*)(?:\.\d*)?|\.\d+)$

这个词组与先前的词组相同。我们只是在前面添加了可选的正负符号

  • **[+-] -**匹配正或负符号。

  • ?- 匹配正号或负号是可选的

让我们也给这个词组添加一个指数部分。再次注意,指数部分是可选的。这个词组与之前的词组相同。我们只是在最后添加了指数部分。

^[+\-]?(?:(?:0|[1-9]\d*)(?:\.\d*)?|\.\d+)(?:\d[eE][+\-]?\d+)?$

让我们来剖析一下指数部分

  • **(?:-**它意味着非捕获组
  • \d- 匹配一个数字。这是为了防止像1.e2这样的数字。
  • [eE]- 匹配小写e或大写E
  • **[+/-] -**匹配正号或负号。匹配正号或负号是可选的
  • **\d+ -**匹配零个或多个数字
  • **)? -**整个搜索表达式是可选的。

程序

现在看看这个正则表达式的操作实例

package main

import (
	"fmt"
	"regexp"
)

func main() {
	sampleRegex := regexp.MustCompile(`^[+\-]?(?:(?:0|[1-9]\d*)(?:\.\d*)?|\.\d+)(?:\d[eE][+\-]?\d+)?$`)

	fmt.Println("Valid Inputs")
	match := sampleRegex.MatchString("1.2")
	fmt.Printf("For 1.2: %t\n", match)

	match = sampleRegex.MatchString(".12")
	fmt.Printf("For .12: %t\n", match)

	match = sampleRegex.MatchString("12")
	fmt.Printf("For 12: %t\n", match)

	match = sampleRegex.MatchString("12.")
	fmt.Printf("For 12.: %t\n", match)

	match = sampleRegex.MatchString("+1.2")
	fmt.Printf("For +1.2.: %t\n", match)

	match = sampleRegex.MatchString("-1.2")
	fmt.Printf("For -1.2.: %t\n", match)

	match = sampleRegex.MatchString("1.2e3")
	fmt.Printf("For 1.2e3.: %t\n", match)

	fmt.Println("\nInValid Inputs")
	match = sampleRegex.MatchString(".")
	fmt.Printf("For .: %t\n", match)

	match = sampleRegex.MatchString("")
	fmt.Printf("For empty string: %t\n", match)

	match = sampleRegex.MatchString("00.1")
	fmt.Printf("For 00.1: %t\n", match)

	match = sampleRegex.MatchString("001")
	fmt.Printf("For 001 %t\n", match)

	match = sampleRegex.MatchString("+")
	fmt.Printf("For +: %t\n", match)

	match = sampleRegex.MatchString("-")
	fmt.Printf("For -: %t\n", match)

	match = sampleRegex.MatchString("+.")
	fmt.Printf("For +.: %t\n", match)

	match = sampleRegex.MatchString("-.")
	fmt.Printf("For -.: %t\n", match)

	match = sampleRegex.MatchString("1.e2")
	fmt.Printf("For 1.e2: %t\n", match)

	match = sampleRegex.MatchString(".e2")
	fmt.Printf("For .e2: %t\n", match)

	match = sampleRegex.MatchString("a1.2")
	fmt.Printf("For a1.2 %t\n", match)

	match = sampleRegex.MatchString("1.2b")
	fmt.Printf("For 1.2b %t\n", match)

	match = sampleRegex.MatchString("a1.2b")
	fmt.Printf("For a1.2b %t\n", match)
}

输出

Valid Inputs
For 1.2: true
For .12: true
For 12: true
For 12.: true
For +1.2.: true
For -1.2.: true
For 1.2e3.: true

InValid Inputs
For .: false
For empty string: false
For 00.1: false
For 001 false
For +: false
For -: false
For +.: false
For -.: false
For 1.e2: false
For .e2: false
For a1.2 false
For 1.2b false
For a1.2b false

对于上面讨论的所有有效输入,程序打印为真

Valid Inputs
For 1.2: true
For .12: true
For 12: true
For 12.: true
For +1.2.: true
For -1.2.: true
For 1.2e3.: true

对于上面讨论的所有无效的输入,它给出的是false。

InValid Inputs
For .: false
For empty string: false
For 00.1: false
For 001 false
For +: false
For -: false
For +.: false
For -.: false
For 1.e2: false
For .e2: false
For a1.2 false
For 1.2b false
For a1.2b false

如果在任何情况下,这个正则表达式不起作用,请试用并在评论中发表。

上面的重码是用来验证一个给定的字符串是否是一个数字。如果你想找到一个输入字符串是否包含一个数字的子串,那么我们需要删除开头和结尾的锚定字符,也就是删除开头的**圆点(^)和结尾的美元($)**字符。

因此,重码将是

[+\-]?(?:(?:0|[1-9]\d*)(?:\.\d*)?|\.\d+)(?:\d[eE][+\-]?\d+)?

这就是关于在golang中通过regex匹配浮点数的全部内容。希望你喜欢这篇文章。请在评论中分享反馈。