I trying to write a ruby program that parses web log and makes sure each part of the log is valid. I trying to deal with the case of in the request string of the log, it has additional double quotes besides the starting and ending ones. I made the web log in the form of a regular expression because it's easier to read that make variables for each part. Here's wut I have so far:
isVal = true
lines = lg.readlines
logLine_regex = /^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3}) - (\w*|-) \[(\d{2})\/(\w{3})\/(\d{4}):(\d{2}):(\d{2}):(\d{2})\s(-0400)\] (".*") (\d+) (\d+|-)$/
lines.each{ |line|
linePos = logLine_regex.match(line)
if linePos == nil
isVal = false
elsif linePos[0] != line.chomp
isVal = false
elsif !((0..255).include?(linePos[1].to_i))
isVal = false
elsif !((0..255).include?(linePos[2].to_i))
isVal = false
elsif !((0..255).include?(linePos[3].to_i))
isVal = false
elsif !((0..255).include?(linePos[4].to_i))
isVal = false
#linePos[5] = Username or hyphen
elsif !((1..31).include?(linePos[6].to_i))
isVal = false
elsif !(["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"].include?(linePos[7]))
isVal = false
elsif !((0..9999).include?(linePos[8].to_i))
isVal = false
elsif !((0..23).include?(linePos[9].to_i))
isVal = false
elsif !((0..59).include?(linePos[10].to_i))
isVal = false
elsif !((0..59).include?(linePos[11].to_i))
isVal = false
#linePos[12] = -4000
#linePos[13] = request
elsif !((0..9999).include?(linePos[14].to_i))
isVal = false
#linePos[15] = bytes
else
isVal = true
end
}
I know that if they are additional double quotes can escape by prefixing it with a backslash, but I have no idea how to code that in ruby. Please help??