Golang中如何确认字符串是否与正则表达式的第一个块匹配
Golang中如何确认字符串是否与正则表达式的第一个块匹配 我有以下代码:
package main
import (
"fmt"
"regexp"
)
func main() {
str := "This is a delimited string, so let's go"
re := regexp.MustCompile(`(?i)(This is a delimited string)|delimited|string`)
matches := re.FindAllString(str, -1)
fmt.Println("We found", len(matches), "matches, that are:", matches)
}
得到的输出是:
We found 1 matches, that are: [This is a delimited string]
如果我将上面代码中的 str 改为:
str := "This is not a delimited string, so let's go"
那么我得到的输出是:
We found 2 matches, that are: [delimited string]
两者都是正确的,但在第一个代码块中,str 有 1 个匹配项,它 100% 匹配我正则表达式中的第一个块,即 This is a delimited string。而第二个代码块中的 str 显示了 2 个匹配项,但其中没有一个匹配我正则表达式中的第一个块。
有没有一种方法可以让我知道 str 是否匹配我正则表达式中的第一个块?这样,当 len(matches) 不为零,但正则表达式的第一个块不匹配时,我就能知道是完全匹配还是部分匹配。
更多关于Golang中如何确认字符串是否与正则表达式的第一个块匹配的实战教程也可以访问 https://www.itying.com/category-94-b0.html
skillian:
是这样吗?
是的,完全正确。我正在尝试构建一个带有搜索引擎的知识库,其工作方式类似于本论坛在发布新问题时显示推荐问题以供参考的方式。
更多关于Golang中如何确认字符串是否与正则表达式的第一个块匹配的实战系列教程也可以访问 https://www.itying.com/category-94-b0.html
根据你最近提出的两个问题,看起来你正在构建一种搜索引擎,当用户搜索某个内容时,你需要同时搜索以下两项:
- 精确匹配(可能在“清理”之后,例如去除多余空格等)
- 包含搜索中的任何“关键词”(例如,忽略“the”、“it”、“a”等词,更具体地搜索其他词;根据你的例子:“delimited”和“string”)
是这样吗?
如果是这样,我建议直接创建独立的正则表达式并手动处理它们的权重。
在Go中,你可以使用FindStringSubmatch方法来检查是否匹配正则表达式中的第一个捕获组。对于你的需求,需要将第一个块作为捕获组,然后检查匹配结果中该捕获组是否非空。
以下是修改后的代码示例:
package main
import (
"fmt"
"regexp"
)
func main() {
// 测试字符串1
str1 := "This is a delimited string, so let's go"
// 测试字符串2
str2 := "This is not a delimited string, so let's go"
// 正则表达式,第一个块作为捕获组
re := regexp.MustCompile(`(?i)((This is a delimited string)|delimited|string)`)
// 测试第一个字符串
matches1 := re.FindStringSubmatch(str1)
fmt.Printf("String 1: %s\n", str1)
if matches1 != nil {
fmt.Printf("Full match: %s\n", matches1[0])
if matches1[1] != "" {
fmt.Println("First block matched!")
} else {
fmt.Println("First block NOT matched")
}
} else {
fmt.Println("No match found")
}
fmt.Println()
// 测试第二个字符串
matches2 := re.FindStringSubmatch(str2)
fmt.Printf("String 2: %s\n", str2)
if matches2 != nil {
fmt.Printf("Full match: %s\n", matches2[0])
if matches2[1] != "" {
fmt.Println("First block matched!")
} else {
fmt.Println("First block NOT matched")
}
} else {
fmt.Println("No match found")
}
}
输出结果:
String 1: This is a delimited string, so let's go
Full match: This is a delimited string
First block matched!
String 2: This is not a delimited string, so let's go
Full match: delimited
First block NOT matched
如果你需要找到所有匹配并检查每个匹配是否来自第一个块,可以使用FindAllStringSubmatch:
package main
import (
"fmt"
"regexp"
)
func main() {
str := "This is not a delimited string, so let's go"
re := regexp.MustCompile(`(?i)((This is a delimited string)|delimited|string)`)
allMatches := re.FindAllStringSubmatch(str, -1)
fmt.Printf("Found %d matches:\n", len(allMatches))
for i, match := range allMatches {
fmt.Printf("Match %d: %s\n", i+1, match[0])
if match[1] != "" {
fmt.Printf(" - Comes from first block: %s\n", match[1])
} else {
fmt.Println(" - Does NOT come from first block")
}
}
}
输出结果:
Found 2 matches:
Match 1: delimited
- Does NOT come from first block
Match 2: string
- Does NOT come from first block
这种方法通过检查捕获组match[1]是否为空来判断匹配是否来自正则表达式中的第一个块。

