runnig

python regex group example

Mar 29th, 2015
333
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.43 KB | None | 0 0
  1. import re
  2.  
  3. template = "(?P<DATE>\d{4}-\d{2}-\d{2}) (?P<TIME>\d\d:\d\d:\d\d) (?P<MODULE>[a-z0-9_]*.py) (?P<LOG_MESSAGE>.*)"
  4. example = "2014-03-01 15:33:43 my_module.py some test log message"
  5.  
  6. matches = re.search(template, example)
  7. date = matches.group('DATE')
  8. time = matches.group('TIME')
  9. module = matches.group('MODULE')
  10. message = matches.group('LOG_MESSAGE')
  11.  
  12. print("DATE:", date, "TIME:", time, "MODULE:", module, "MSG:", message)
Advertisement
Add Comment
Please, Sign In to add comment