RegEx for parsing log files


/ Published in: Other
Save to your folder(s)



Copy this code and paste it in your HTML
  1. Complete Expression for a standard stats log
  2.  
  3. ^(?<Date>(?<Year>\d{4}|\d{2})-(?<Month>\d{1,2})-(?<Day>\d{1,2}))\s(?<Time>(?<Hour>\d{1,2}):(?<Minute>\d{1,2}):(?<Second>\d{1,2}))\s(?<ClientIP>(?<A>\d{1,3})\.(?<B>\d{1,3})\.(?<C>\d{1,3})\.(?<D>\d{1,3}))\s(?<Username>[\w-]*)\s(?<Method>\w*)\s(?<URI>[\w/-_\.+]*)\s(?<StatusCode>\d{3})\s(?<Bytes>\d*)\s(?<Version>[\w/-_\.+]*)\s(?<UserAgent>[\w/-_\.+;()]*|-)\s(?<Referer>[\w\.-_://]*|-)
  4.  
  5. Complete Expression for a standard stats log with URI broken into prefix and extension
  6.  
  7. ^(?<Date>(?<Year>\d{4}|\d{2})-(?<Month>\d{1,2})-(?<Day>\d{1,2}))\s(?<Time>(?<Hour>\d{1,2}):(?<Minute>\d{1,2}):(?<Second>\d{1,2}))\s(?<ClientIP>(?<A>\d{1,3})\.(?<B>\d{1,3})\.(?<C>\d{1,3})\.(?<D>\d{1,3}))\s(?<Username>[\w-]*)\s(?<Method>\w*)\s(?<URI>(?<Prefix>[\w/-_\.+]*)\.(?<Ext>\w{0,8}))\s(?<StatusCode>\d{3})\s(?<Bytes>\d*)\s(?<Version>[\w/-_\.+]*)\s(?<UserAgent>[\w/-_\.+;()]*|-)\s(?<Referer>[\w\.-_://]*|-)
  8.  
  9.  
  10. Pieces:
  11.  
  12.  
  13. Date
  14. (?<Date>(?<Year>\d{4}|\d{2})-(?<Month>\d{1,2})-(?<Day>\d{1,2}))
  15.  
  16. Time
  17. (?<Time>(?<Hour>\d{1,2}):(?<Minute>\d{1,2}):(?<Second>\d{1,2}))
  18.  
  19. IP Address
  20. (?<ClientIP>(?<A>\d{1,3})\.(?<B>\d{1,3})\.(?<C>\d{1,3})\.(?<D>\d{1,3}))
  21.  
  22. UserName
  23. (?<Username>[\w-]*)
  24.  
  25.  
  26. Method
  27. (?<Method>\w*)
  28.  
  29. URI
  30. (?<URI>[\w/-_\.+]*)
  31. (?<URI>(?<Prefix>[\w/-_\.+]*)\.(?<Ext>\w{0,8}))
  32.  
  33. StatusCode
  34. (?<StatusCode>\d{3})
  35.  
  36. Bytes
  37. (?<Bytes>\d*)
  38.  
  39.  
  40. Version
  41. (?<Version>[\w/-_\.+]*)
  42.  
  43. UserAgent
  44. (?<UserAgent>[\w/-_\.+;()]*|-)
  45.  
  46. Referrer
  47. (?<Referer>[\w\.-_://]*|-)

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.