


Regular expression with golang to match complete lines containing 'error' or 'warning' (case insensitive)
php editor Baicao today introduces how to use regular expressions to match complete lines containing "error" or "warning" (case-insensitive) in golang. Regular expressions are a powerful text pattern matching tool that can help us find content that matches a specific pattern in a string. In golang, using regular expressions requires introducing the regexp package and using the Compile function to compile the regular expression. Next, we will introduce in detail how to use regular expressions for line matching in golang.
Question content
I want to print the complete line of each line in the log file that contains warn or error (case-insensitive) to the user.
Therefore:
[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
I want:
[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
I started innocently
errorregex := regexp.mustcompile(`(?is)error|warn`)
It will only print (from a different run, may not match the example above exactly)
warn error
Then I thought I should change it to match more:
errorRegEx := regexp.MustCompile(`(?is).*error.*|.*warn.*`)
But this doesn’t print anything at all
How to get the complete line and all lines where warn or error (case insensitive) will match?
ps: This is different from the suggested regex matching lines containing string, as this is a question specifically for the go
language, which doesn't seem to use the exact same standard engine.
Solution
Considering that the question has been marked as a dupe, the op's comments are as follows.
This question is marked as a duplicate, and the linked post has a lot of answers that we can use to try to piece together an answer to the OP's question, but it's still not complete as the answers seem to be related to pcre and go using 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) }
First of all, I don't understand why this prints nothing for op:
Then I thought I should change it to match more:
errorregex := regexp.mustcompile(`(?is).*error.*|.*warn.*`)
But this doesn’t print anything at all
Because all contents should be printed:
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"
s flag in
(?is)...
means to match newlines with dots (.
)^1 , and because your stars (*
) are greedy^2, they will match the entire string if an "error" or "warning" is found All content.
The real solution is to not match "\n" with dots - remove the s
flag and you get what you want:
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"
Now we match between instances of "\n" (valid rows), because we use the all
form, which only looks for non-overlapping matches:
If "all" is present, the routine will match consecutive non-overlapping matches of the entire expression. ^3< /a>
We get complete and clear lines.
You can tighten this regex slightly:
`(?i).*(?:error|warn).*` // "anything before either "error" or "warn" and anything after (for a line)"
(?:...)
is a non-capturing group^1 since you don't seem to care about individual "bugs" in each match or "warning" instance.
Also, I still want to show that splitting by line before trying to match gives you more control/precision and makes the regex very easy to reason about:
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"
All three examples are located in this playground.
The above is the detailed content of Regular expression with golang to match complete lines containing 'error' or 'warning' (case insensitive). For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











Yes, H5 page production is an important implementation method for front-end development, involving core technologies such as HTML, CSS and JavaScript. Developers build dynamic and powerful H5 pages by cleverly combining these technologies, such as using the <canvas> tag to draw graphics or using JavaScript to control interaction behavior.

The method of customizing resize symbols in CSS is unified with background colors. In daily development, we often encounter situations where we need to customize user interface details, such as adjusting...

How to achieve the 45-degree curve effect of segmenter? In the process of implementing the segmenter, how to make the right border turn into a 45-degree curve when clicking the left button, and the point...

How to use JavaScript or CSS to control the top and end of the page in the browser's printing settings. In the browser's printing settings, there is an option to control whether the display is...

Regarding the reasons and solutions for misaligned display of inline-block elements. When writing web page layout, we often encounter some seemingly strange display problems. Compare...

Real-time Bitcoin USD Price Factors that affect Bitcoin price Indicators for predicting future Bitcoin prices Here are some key information about the price of Bitcoin in 2018-2024:

Tips for Implementing Segmenter Effects In user interface design, segmenter is a common navigation element, especially in mobile applications and responsive web pages. ...

Compatibility issues of multi-row overflow on mobile terminal omitted on different devices When developing mobile applications using Vue 2.0, you often encounter the need to overflow text...
