Created
November 30, 2011 19:07
-
-
Save sgonyea/1410347 to your computer and use it in GitHub Desktop.
A documented regex, Ruby. You can do this. It's great.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
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
suggest use \A and \z as ruby regex start and end.