Apache/Nginx Combined Log Format Regex for Go
/^(\S+) \S+ (\S+) \[([^\]]+)\] "([A-Z]+) ([^"]+) HTTP/[\d.]+" ([1-5]\d{2}) (\d+|-)(?:\s"([^"]*)" "([^"]*)")?/What this pattern does
This page provides a comprehensive, battle-tested regular expression for matching apache/nginx combined log format, ported and verified for Go. A rigorously tested regex reduces debugging time and protects your application from edge-case failures. The snippet below is ready to drop into your Go project — whether you're validating in a Gin handler, a gRPC service, or a command-line tool.
Go Implementation
// Apache/Nginx Combined Log Format
// ReDoS-safe | RegexVault — Dev & Systems > Log Parsing
package validation
import "regexp"
var apachenginxCombinedLogFormatRe = regexp.MustCompile(`^(\S+) \S+ (\S+) \[([^\]]+)\] "([A-Z]+) ([^"]+) HTTP/[\d.]+" ([1-5]\d{2}) (\d+|-)(?:\s"([^"]*)" "([^"]*)")?`)
func ValidateApachenginxCombinedLogFormat(s string) bool {
return apachenginxCombinedLogFormatRe.MatchString(s)
}
// Example
// fmt.Println(ValidateApachenginxCombinedLogFormat("192.168.1.1 - frank [10/Oct/2000:13:55:36 -0700] "GET /apache_pb.gif HTTP/1.0" 200 2326 "http://www.example.com/start.html" "Mozilla/5.0"")) // trueTest Cases
Matches (Valid) | Rejects (Invalid) |
|---|---|
192.168.1.1 - frank [10/Oct/2000:13:55:36 -0700] "GET /apache_pb.gif HTTP/1.0" 200 2326 "http://www.example.com/start.html" "Mozilla/5.0" | not a log line |
10.0.0.1 - - [01/Jan/2024:00:00:00 +0000] "POST /api/v1/users HTTP/1.1" 201 450 | 192.168.1.1 [date] GET / 200 |
| — | plain text log |
When to use this pattern
This pattern is drawn from the Dev & Systems > Log Parsing category and carries a ReDoS-safe certification. That matters for Go developers because Go's RE2 engine is inherently safe from catastrophic backtracking, but this pattern has been additionally verified for correctness. RegexVault audits patterns against known backtracking attack vectors, ensuring you have the necessary context before using this regex in a high-stakes production environment.
Common Pitfalls
Log lines with escaped quotes (\" inside request field) will break naive parsers. Always handle escaped quotes inside quoted fields.
Technical Notes
Groups: 1=client IP, 2=auth user, 3=datetime, 4=method, 5=path, 6=status, 7=bytes, 8=referer, 9=user-agent. The - placeholder indicates missing values.
Have a pattern that belongs in the vault?
Submit it for review — community-verified patterns get credited to your GitHub handle. Free submissions join the queue. Priority review available for $15.
Submit a Pattern