Table of Contents
Question content
Solution
Home Backend Development Golang Regular expression with golang to match complete lines containing 'error' or 'warning' (case insensitive)

Regular expression with golang to match complete lines containing 'error' or 'warning' (case insensitive)

Feb 13, 2024 am 08:21 AM
overflow

正则表达式与 golang 匹配包含“错误”或“警告”(不区分大小写)的完整行

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
Copy after login

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
Copy after login

I started innocently

errorregex := regexp.mustcompile(`(?is)error|warn`)
Copy after login

It will only print (from a different run, may not match the example above exactly)

warn
error
Copy after login

Then I thought I should change it to match more:

errorRegEx := regexp.MustCompile(`(?is).*error.*|.*warn.*`)
Copy after login

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)
}
Copy after login

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.*`)
Copy after login

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)
}
Copy after login
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"
Copy after login
The

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)
}
Copy after login
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"
Copy after login

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)"
Copy after login

(?:...) 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++
}
Copy after login
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"
Copy after login

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!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1662
14
PHP Tutorial
1262
29
C# Tutorial
1234
24
Is H5 page production a front-end development? Is H5 page production a front-end development? Apr 05, 2025 pm 11:42 PM

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 &lt;canvas&gt; tag to draw graphics or using JavaScript to control interaction behavior.

How to customize the resize symbol through CSS and make it uniform with the background color? How to customize the resize symbol through CSS and make it uniform with the background color? Apr 05, 2025 pm 02:30 PM

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 use the clip-path attribute of CSS to achieve the 45-degree curve effect of segmenter? How to use the clip-path attribute of CSS to achieve the 45-degree curve effect of segmenter? Apr 04, 2025 pm 11:45 PM

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 control the top and end of pages in browser printing settings through JavaScript or CSS? How to control the top and end of pages in browser printing settings through JavaScript or CSS? Apr 05, 2025 pm 10:39 PM

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...

Why are the inline-block elements misaligned? How to solve this problem? Why are the inline-block elements misaligned? How to solve this problem? Apr 04, 2025 pm 10:39 PM

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...

The latest price of Bitcoin in 2018-2024 USD The latest price of Bitcoin in 2018-2024 USD Feb 15, 2025 pm 07:12 PM

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:

How to achieve segmentation effect with 45 degree curve border? How to achieve segmentation effect with 45 degree curve border? Apr 04, 2025 pm 11:48 PM

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

How to compatible with multi-line overflow omission on mobile terminal? How to compatible with multi-line overflow omission on mobile terminal? Apr 05, 2025 pm 10:36 PM

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...

See all articles