# coding=utf8
# the above tag defines encoding for this document and is for Python 2.x compatibility
import re
regex = r"(?P<timestamp>\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2},\d{3}) (?P<level>INFO|ERROR|WARN|TRACE|DEBUG|FATAL)\s+\[(?P<firstSquareBrackets>[^\]]*)]\s+\[(?P<secondSquareBrackets>[^\]]*)]\s+\[(?P<thirdSquareBrackets>[^\]]*)]\s+\[(?P<fourthSquareBrackets>[^\]]*)]\s+\[(?P<fifthSquareBrackets>[^\]]*)] - (?P<textMessage>.*?)(?=\\Z|;\s+|\n)|(?P<exception>^.+Exception: .+)|(?P<stacktrace>^\s+at .+)|(^\s+... \d+ more)|(^\s*Caused by:.+)"
test_str = ("2018-07-24 00:01:33,711 WARN [r3223r2r3] [] [] [ewqrwerwer] [ewfwefwef] - Failed;\n"
"2018-07-24 00:01:33,712 DEBUG [r3223r2r3] [] [] [ewqrwerwer] [ewfwefwef] - Sample log\n"
"2018-07-24 00:02:33,712 DEBUG [r3223r2r3] [] [] [ewqrwerwer] [ewfwefwef] - Sample log\n"
"2018-07-24 00:20:33,830 DEBUG [r3223r2r3] [] [] [ewqrwerwer] [ewfwefwef] - Sample log\n"
"2018-07-24 00:20:37,731 DEBUG [r3223r2r3] [] [] [3r2r32r] [c.i.s.h.r.i.RssNewsServiceImpl] - 3r232r\n"
"2018-07-24 10:33:45,852 ERROR [r3223r2r3] [fgd] [gr] [reger] [regre] - Sample log\n"
"javax.servlet.ServletException: Something bad happened\n"
" at com.example.myproject.OpenSessionInViewFilter.doFilter(OpenSessionInViewFilter.java:60)\n"
" at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)\n"
" at com.example.myproject.ExceptionHandlerFilter.doFilter(ExceptionHandlerFilter.java:28)\n"
" at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)\n"
" at com.example.myproject.OutputBufferFilter.doFilter(OutputBufferFilter.java:33)\n"
" at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)\n"
" at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:388)\n"
" at org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216)\n"
" at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:182)\n"
" at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:765)\n"
" at org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:418)\n"
" at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)\n"
" at org.mortbay.jetty.Server.handle(Server.java:326)\n"
" at org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:542)\n"
" at org.mortbay.jetty.HttpConnection$RequestHandler.content(HttpConnection.java:943)\n"
" at org.mortbay.jetty.HttpParser.parseNext(HttpParser.java:756)\n"
" at org.mortbay.jetty.HttpParser.parseAvailable(HttpParser.java:218)\n"
" at org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:404)\n"
" at org.mortbay.jetty.bio.SocketConnector$Connection.run(SocketConnector.java:228)\n"
" at org.mortbay.thread.QueuedThreadPool$PoolThread.run(QueuedThreadPool.java:582)\n"
"Caused by: com.example.myproject.MyProjectServletException\n"
" at com.example.myproject.MyServlet.doPost(MyServlet.java:169)\n"
" at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)\n"
" at javax.servlet.http.HttpServlet.service(HttpServlet.java:820)\n"
" at org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:511)")
matches = re.finditer(regex, test_str, re.MULTILINE)
for matchNum, match in enumerate(matches, start=1):
print ("Match {matchNum} was found at {start}-{end}: {match}".format(matchNum = matchNum, start = match.start(), end = match.end(), match = match.group()))
for groupNum in range(0, len(match.groups())):
groupNum = groupNum + 1
print ("Group {groupNum} found at {start}-{end}: {group}".format(groupNum = groupNum, start = match.start(groupNum), end = match.end(groupNum), group = match.group(groupNum)))
# Note: for Python 2.7 compatibility, use ur"" to prefix the regex and u"" to prefix the test string and substitution.
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for Python, please visit: https://docs.python.org/3/library/re.html