Java Regex-匹配tomcat日志文件中的条目
我试图一个接一个地读取每个日志条目。这是日志文件的一部分:Java Regex-匹配tomcat日志文件中的条目,java,regex,Java,Regex,我试图一个接一个地读取每个日志条目。这是日志文件的一部分: at java.net.Socket.<init>(Socket.java:434) at java.net.Socket.<init>(Socket.java:211) at sun.rmi.transport.proxy.RMIDirectSocketFactory.createSocket(RMIDirectSocketFactory.java:40) at sun.rmi.tran
at java.net.Socket.<init>(Socket.java:434)
at java.net.Socket.<init>(Socket.java:211)
at sun.rmi.transport.proxy.RMIDirectSocketFactory.createSocket(RMIDirectSocketFactory.java:40)
at sun.rmi.transport.proxy.RMIMasterSocketFactory.createSocket(RMIMasterSocketFactory.java:148)
at sun.rmi.transport.tcp.TCPEndpoint.newSocket(TCPEndpoint.java:613)
... 31 more
26.03.2018 14:43:57,113| INFO http-nio-8080-exec-10 configService==nullLooking up configuration service on rmi://localhost:1199/ConfigService |com.ase.common.utils.ConfigurationServiceUtils
26.03.2018 14:43:57,113| WARN http-nio-8080-exec-10 Could not connect to services. |com.ase.common.utils.ConfigurationServiceUtils
java.rmi.ConnectException: Connection refused to host: localhost; nested exception is:
java.net.ConnectException: Connection refused
at sun.rmi.transport.tcp.TCPEndpoint.newSocket(TCPEndpoint.java:619)
at sun.rmi.transport.tcp.TCPChannel.createConnection(TCPChannel.java:216)
at sun.rmi.transport.tcp.TCPChannel.newConnection(TCPChannel.java:202)
at sun.rmi.server.UnicastRef.newCall(UnicastRef.java:342)
at sun.rmi.registry.RegistryImpl_Stub.lookup(Unknown Source)
它匹配ok所有条目,但没有stacktrace。我需要如何改进我的正则表达式以获得堆栈跟踪
所以我需要这样做:
Match1 : `26.03.2018 14:43:57,113| INFO http-nio-8080-exec-10 configService==nullLooking up configuration service on rmi://localhost:1199/ConfigService |com.ase.common.utils.ConfigurationServiceUtils`
Group(1)-> `26.03.2018 14:43:57,113`; Group(2)->`INFO`; Group(3)-> `http-nio-8080-exec-10`; Group(4)->`configService==nullLooking up configuration service on rmi://localhost:1199/ConfigService`;
Group(5)->`com.ase.common.utils.ConfigurationServiceUtils`
Match2 : `26.03.2018 14:43:57,113| WARN http-nio-8080-exec-10 Could not connect to services. |com.ase.common.utils.ConfigurationServiceUtils
java.rmi.ConnectException: Connection refused to host: localhost; nested exception is:
java.net.ConnectException: Connection refused
at sun.rmi.transport.tcp.TCPEndpoint.newSocket(TCPEndpoint.java:619)
at sun.rmi.transport.tcp.TCPChannel.createConnection(TCPChannel.java:216)
at sun.rmi.transport.tcp.TCPChannel.newConnection(TCPChannel.java:202)
at sun.rmi.server.UnicastRef.newCall(UnicastRef.java:342)
at sun.rmi.registry.RegistryImpl_Stub.lookup(Unknown Source) `
Group(1)-> `26.03.2018 14:43:57,113`; Group(2)->`WARN`; Group(3)-> `http-nio-8080-exec-10`; Group(4)->`Could not connect to services.`;
Group(5)->`com.ase.common.utils.ConfigurationServiceUtils`; Group(6)->
java.rmi.ConnectException: Connection refused to host: localhost; nested exception is:
java.net.ConnectException: Connection refused...
您应该在正则表达式中使用更具体的捕获组,我将指出:
^(\d+\.\d+\.\d{4}[^|]+)\|\s+(\S+)\s+(\S+)\s+([^|]+)\|(\S+)\s+((?:(?!^\d+\.)[^|])*)
细分:
匹配以日期开头的行(字幕组#1)^(\d+\.\d+\.\d{4}[^ |]+)\
匹配空格并捕获以下非空白字符(CG#2)\s+(\s+)
相同(CG#3)\s+(\s+)
匹配空格和除\s+([^ |]+)\\\\\\
(CG#3)以外的任何内容
匹配并捕获后跟空格的非空白字符(CG#4)(\S+)\S+
回火模式,用于检查是否位于应作为下一个匹配开始的行的开头。如果不匹配下一个直接字符(CG#5,可选组)((?:(?!^\d+\)[^ |]*)
准确显示您要匹配的内容(边界是什么)以及是否要捕获内容。编辑我的答案。请检查此感谢!很好!
^(\d+\.\d+\.\d{4}[^|]+)\|\s+(\S+)\s+(\S+)\s+([^|]+)\|(\S+)\s+((?:(?!^\d+\.)[^|])*)