php小编百草今天为大家介绍如何使用正则表达式在golang中匹配包含"错误"或"警告"(不区分大小写)的完整行。正则表达式是一种强大的文本模式匹配工具,可以帮助我们在字符串中找到符合特定模式的内容。在golang中,使用正则表达式需要引入regexp包,并使用Compile函数编译正则表达式。接下来,我们将详细介绍如何在golang中使用正则表达式进行行匹配。
问题内容
我想将日志文件中包含 warn 或 error(不区分大小写)的每一行的完整行打印给用户。
鉴于此:
[01-17|18:53:38.179] info server/server.go:381 this would be skipped
[01-17|18:53:38.280] info server/server.go:620 this also
[01-17|18:53:41.180] warn server/server.go:388 something is warned, so show this
[01-17|18:53:41.394] warn server/server.go:188 something reported an ->error<-
[01-17|18:53:41.395] error server/server.go:191 blabla
[01-17|18:53:41.395] debug server/server.go:196 obviously skipped
[01-17|18:53:41.395] debug server/server.go:196 this debug contains an ->error<- so match this
[01-17|18:53:41.395] warn server/server.go:198 you get the idea
我想要:
[01-17|18:53:41.180] warn server/server.go:388 something is warned, so show this
[01-17|18:53:41.394] warn server/server.go:188 something reported an ->error<-
[01-17|18:53:41.395] error server/server.go:191 blabla
[01-17|18:53:41.395] debug server/server.go:196 this debug contains an ->error<- so match this
[01-17|18:53:41.395] warn server/server.go:198 you get the idea
我天真地开始了
errorregex := regexp.mustcompile(`(?is)error|warn`)
它只会打印(来自不同的运行,可能与上面的示例不完全匹配)
warn
error
然后我想我应该改变它以匹配更多:
errorRegEx := regexp.MustCompile(`(?is).*error.*|.*warn.*`)
但这根本没有打印任何内容
如何获得完整的行以及所有行,其中 warn 或 error(不区分大小写)将匹配?
ps:这与建议的包含 string 的正则表达式匹配行不同,因为这是专门针对 go
语言的问题,它似乎没有使用完全相同的标准引擎。
解决方法
考虑到该问题已被标记为欺骗,op 的评论如下。
这个问题被标记为重复,并且该链接的帖子有很多答案,我们可以用它们来尝试拼凑出op问题的答案,但仍然不完全,因为这些答案似乎与 pcre 和 go 相关使用 re2。
var logs = `
[01-17|18:53:38.179] info server/server.go:381 this would be skipped
[01-17|18:53:38.280] info server/server.go:620 this also
[01-17|18:53:41.180] warn server/server.go:388 something is warned, so show this
[01-17|18:53:41.394] warn server/server.go:188 something reported an ->error<-
[01-17|18:53:41.395] error server/server.go:191 blabla
[01-17|18:53:41.395] debug server/server.go:196 obviously skipped
[01-17|18:53:41.395] debug server/server.go:196 this debug contains an ->error<- so match this
[01-17|18:53:41.395] warn server/server.go:198 you get the idea
`
func init() {
logs = strings.trimspace(logs)
}
首先,我不明白为什么这没有为 op 打印任何内容:
然后我想我应该改变它以匹配更多:
errorregex := regexp.mustcompile(`(?is).*error.*|.*warn.*`)
但这根本没有打印任何内容
因为应该打印所有内容:
fmt.println("original regexp:")
reoriginal := regexp.mustcompile(`(?is).*error.*|.*warn.*`)
lines := reoriginal.findallstring(logs, -1)
fmt.println("match\t\tentry")
fmt.println("=====\t\t=====")
for i, line := range lines {
fmt.printf("%d\t\t%q\n", i+1, line)
}
original regexp:
match entry
===== =====
1 "[01-17|18:53:38.179] info server/server.go:381 this would be skipped\n[01-17|18:53:38.280] info server/server.go:620 this also\n[01-17|18:53:41.180] warn server/server.go:388 something is warned, so show this\n[01-17|18:53:41.394] warn server/server.go:188 something reported an ->error<-\n[01-17|18:53:41.395] error server/server.go:191 blabla\n[01-17|18:53:41.395] debug server/server.go:196 obviously skipped\n[01-17|18:53:41.395] debug server/server.go:196 this debug contains an ->error<- so match this\n[01-17|18:53:41.395] warn server/server.go:198 you get the idea"
(?is)...
中的 s
标志表示将换行符与点匹配 (.
)^1,并且因为您的星星 (*
) 是贪婪^2,如果发现“错误”或“警告”,它们将匹配整个字符串中的所有内容。
真正的解决方案就是不将“\n”与点匹配 - 去掉 s
标志,您就得到了您想要的结果:
fmt.println("whole text:")
rewholetext := regexp.mustcompile(`(?i).*error.*|.*warn.*`)
lines = rewholetext.findallstring(logs, -1)
fmt.println("match\t\tentry")
fmt.println("=====\t\t=====")
for i, line := range lines {
fmt.printf("%d\t\t%q\n", i+1, line)
}
whole text:
match entry
===== =====
1 "[01-17|18:53:41.180] warn server/server.go:388 something is warned, so show this"
2 "[01-17|18:53:41.394] warn server/server.go:188 something reported an ->error<-"
3 "[01-17|18:53:41.395] error server/server.go:191 blabla"
4 "[01-17|18:53:41.395] debug server/server.go:196 this debug contains an ->error<- so match this"
5 "[01-17|18:53:41.395] warn server/server.go:198 you get the idea"
现在我们在“\n”实例(有效的行)之间进行匹配,因为我们使用 all
表单,它只查找非重叠匹配:
如果存在“all”,则例程将匹配整个表达式的连续非重叠匹配。^3< /a>
我们得到完整且清晰的线条。
您可以稍微收紧该正则表达式:
`(?i).*(?:error|warn).*` // "anything before either "error" or "warn" and anything after (for a line)"
(?:...)
是一个非捕获组^1 因为您似乎并不关心每场比赛中个别的“错误”或“警告”实例。
而且,我仍然想表明,在尝试匹配之前按行分割可以为您提供更多控制/精度,并使正则表达式非常容易推理:
r := strings.newreader(logs)
scanner := bufio.newscanner(r)
fmt.println("line-by-line:")
reline := regexp.mustcompile(`(?i)error|warn`)
fmt.println("match\tline\tentry")
fmt.println("=====\t====\t=====")
var matchno, lineno, match = 1, 1, ""
for scanner.scan() {
line := scanner.text()
match = reline.findstring(line)
if match != "" {
fmt.printf("%d\t%d\t%q\n", matchno, lineno, line)
matchno++
}
lineno++
}
Line-by-line:
match line entry
===== ==== =====
1 3 "[01-17|18:53:41.180] Warn server/server.go:388 Something is warned, so show this"
2 4 "[01-17|18:53:41.394] warn server/server.go:188 Something reported an ->error<-"
3 5 "[01-17|18:53:41.395] Error server/server.go:191 Blabla"
4 7 "[01-17|18:53:41.395] DEBUG server/server.go:196 This debug contains an ->error<- so match this"
5 8 "[01-17|18:53:41.395] WARN server/server.go:198 You get the idea"
所有三个示例均位于此 playground 中。
以上就是正则表达式与 golang 匹配包含“错误”或“警告”(不区分大小写)的完整行的详细内容,更多请关注编程网其它相关文章!