We Recommend

Learning Python Learning Python
The authors of Learning Python show you enough essentials of the Python scripting language to enable you to begin solving problems right away, then reveal more powerful aspects of the language one at a time. This approach is sure to appeal to programmers and system administrators who have urgent problems and a preference for learning by semi-guided experimentation.


Posted By

roberocity on 10/10/06


Tagged

regex general


Versions (?)


Who likes this?

1 person has marked this snippet as a favorite

zingo


RegEx for parsing log files


Published in: Other 


  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 

You need to login to post a comment.