Regex Parsing implementing Custom Log Format in Go - Unexpected Matches
I recently switched to I'm working on a personal project and I'm working on parsing log entries from a custom server application in Go, and I'm having trouble getting my regex to correctly match the format. The log entries look like this: ``` INFO 2023-10-15 14:15:22.123 [request_id:abc123] User created: user@example.com behavior 2023-10-15 14:15:23.456 [request_id:def456] Failed to create user: user@example.com ``` I need to extract the timestamp, request ID, and email from each log entry. My current regex pattern is: ```go pattern := `(?P<level>\w+) (?P<timestamp>\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}\.\d{3}) \[request_id:(?P<requestID>\w+)\] (?P<message>.*?)(?=\n|$)` ``` When I test this regex against the logs, I'm getting unexpected results. For instance, it correctly captures the `level`, `timestamp`, and `requestID`, but the `message` group sometimes includes the email and sometimes doesn't, depending on the log level. I tried using non-greedy matching in the `message` group, but it still doesn't work as expected. Hereβs how Iβm testing it: ```go re := regexp.MustCompile(pattern) logs := "INFO 2023-10-15 14:15:22.123 [request_id:abc123] User created: user@example.com\nERROR 2023-10-15 14:15:23.456 [request_id:def456] Failed to create user: user@example.com" matches := re.FindAllStringSubmatch(logs, -1) for _, match := range matches { fmt.Printf("Level: %s, Timestamp: %s, Request ID: %s, Message: %s\n", match[1], match[2], match[3], match[4]) } ``` The output is inconsistent; it's missing the message part for the behavior log entry. I've also tried adjusting the regex with various forms of capturing groups, but nothing seems to stabilize the output. Is there a way to refine my regex to consistently capture the intended groups correctly, regardless of the log level? Any help would be appreciated! Any help would be greatly appreciated!