use strict;
my $str = '2018-07-24 00:01:33,711 WARN [r3223r2r3] [] [] [ewqrwerwer] [ewfwefwef] - Failed;
2018-07-24 00:01:33,712 DEBUG [r3223r2r3] [] [] [ewqrwerwer] [ewfwefwef] - Sample log
2018-07-24 00:02:33,712 DEBUG [r3223r2r3] [] [] [ewqrwerwer] [ewfwefwef] - Sample log
2018-07-24 00:20:33,830 DEBUG [r3223r2r3] [] [] [ewqrwerwer] [ewfwefwef] - Sample log
2018-07-24 00:20:37,731 DEBUG [r3223r2r3] [] [] [3r2r32r] [c.i.s.h.r.i.RssNewsServiceImpl] - 3r232r
2018-07-24 10:33:45,852 ERROR [r3223r2r3] [fgd] [gr] [reger] [regre] - Sample log
javax.servlet.ServletException: Something bad happened
at com.example.myproject.OpenSessionInViewFilter.doFilter(OpenSessionInViewFilter.java:60)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
at com.example.myproject.ExceptionHandlerFilter.doFilter(ExceptionHandlerFilter.java:28)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
at com.example.myproject.OutputBufferFilter.doFilter(OutputBufferFilter.java:33)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:388)
at org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216)
at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:182)
at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:765)
at org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:418)
at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
at org.mortbay.jetty.Server.handle(Server.java:326)
at org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:542)
at org.mortbay.jetty.HttpConnection$RequestHandler.content(HttpConnection.java:943)
at org.mortbay.jetty.HttpParser.parseNext(HttpParser.java:756)
at org.mortbay.jetty.HttpParser.parseAvailable(HttpParser.java:218)
at org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:404)
at org.mortbay.jetty.bio.SocketConnector$Connection.run(SocketConnector.java:228)
at org.mortbay.thread.QueuedThreadPool$PoolThread.run(QueuedThreadPool.java:582)
Caused by: com.example.myproject.MyProjectServletException
at com.example.myproject.MyServlet.doPost(MyServlet.java:169)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:820)
at org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:511)';
my $regex = qr/(?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:.+)/mp;
if ( $str =~ /$regex/g ) {
print "Whole match is ${^MATCH} and its start/end positions can be obtained via \$-[0] and \$+[0]\n";
# print "Capture Group 1 is $1 and its start/end positions can be obtained via \$-[1] and \$+[1]\n";
# print "Capture Group 2 is $2 ... and so on\n";
}
# ${^POSTMATCH} and ${^PREMATCH} are also available with the use of '/p'
# Named capture groups can be called via $+{name}
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 Perl, please visit: http://perldoc.perl.org/perlre.html