Skip to content

Instantly share code, notes, and snippets.

@sgonyea
Created November 30, 2011 19:07
Show Gist options
  • Save sgonyea/1410347 to your computer and use it in GitHub Desktop.
Save sgonyea/1410347 to your computer and use it in GitHub Desktop.
A documented regex, Ruby. You can do this. It's great.
def log_regex
@log_regex ||= begin
ip_regex = '(?:\d+\.){3}\d+'
log_regex = /^
(#{ip_regex})\s-\s-\s # IP
\[(.*?)\]\s # Date
"(.*?)"\s # Request
(\d+)\s # Response Code
(\d+)\s # Bytes Sent by Server
([\d\.]+)\s # Request Time
"(.*?)"\s # Referrer
"(.*?)"\s # User Agent
"(.*?)" # http-x-forwarded-for header
$/mx
end
end
@xiaods
Copy link

xiaods commented Mar 6, 2012

suggest use \A and \z as ruby regex start and end.

@sgonyea
Copy link
Author

sgonyea commented Mar 7, 2012

Not necessarily! This is to scan through multiple lines (records) of string data, using StringScanner. If the string were split by lines, then that would work. For the most part, \A and \Z are for security / über strict matching -- ie, you're validating an e-mail address. Never mind whether or not a regex is the right tool for THAT job :).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment