I'm trying to extract the value from the exception by using regular expression in Java. But the string is too complicated and long (all in one line):
ReturnCode={Val=9002;SubVal=9203;Text=Subscriber not found};Message=Subscriber not found;LocalizedMessage=Subscriber not found;Cause==null;StackTrace={[co.com.tigo.gatewaytigo.scp.mcommerce.method.RechargeMCommerce.getRechargeResult(RechargeMCommerce.java:72)][co.com.tigo.gatewaytigo.scp.mcommerce.MCommerceAPI.recharge(MCommerceAPI.java:353)][co.com.tigo.gatewaytigo.scp.interfacesscp.SCPFacade.recharge(SCPFacade.java:257)][co.com.tigo.gatewaytigo.webscp.ws.GatewayTigoWS.recharge(Unknown Source)][sun.reflect.GeneratedMethodAccessor4092.invoke(Unknown Source)][sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)][java.lang.reflect.Method.invoke(Method.java:597)][weblogic.wsee.jaxws.WLSInstanceResolver$WLSInvoker.invoke(WLSInstanceResolver.java:89)][weblo开发者_运维知识库gic.wsee.jaxws.WLSInstanceResolver$WLSInvoker.invoke(WLSInstanceResolver.java:71)][com.sun.xml.ws.server.InvokerTube$2.invoke(InvokerTube.java:146)][com.sun.xml.ws.server.sei.EndpointMethodHandler.invoke(EndpointMethodHandler.java:257)][com.sun.xml.ws.server.sei.SEIInvokerTube.processRequest(SEIInvokerTube.java:93)][com.sun.xml.ws.api.pipe.Fiber.__doRun(Fiber.java:598)][com.sun.xml.ws.api.pipe.Fiber._doRun(Fiber.java:557)][com.sun.xml.ws.api.pipe.Fiber.doRun(Fiber.java:542)][com.sun.xml.ws.api.pipe.Fiber.runSync(Fiber.java:439)][com.sun.xml.ws.server.WSEndpointImpl$2.process(WSEndpointImpl.java:243)][com.sun.xml.ws.transport.http.HttpAdapter$HttpToolkit.handle(HttpAdapter.java:444)][com.sun.xml.ws.transport.http.HttpAdapter.handle(HttpAdapter.java:244)][com.sun.xml.ws.transport.http.servlet.ServletAdapter.handle(ServletAdapter.java:134)][weblogic.wsee.jaxws.HttpServletAdapter$AuthorizedInvoke.run(HttpServletAdapter.java:272)][weblogic.wsee.jaxws.HttpServletAdapter.post(HttpServletAdapter.java:185)][weblogic.wsee.jaxws.JAXWSServlet.doPost(JAXWSServlet.java:180)][javax.servlet.http.HttpServlet.service(HttpServlet.java:727)][weblogic.wsee.jaxws.JAXWSServlet.service(JAXWSServlet.java:64)][javax.servlet.http.HttpServlet.service(HttpServlet.java:820)][weblogic.servlet.internal.StubSecurityHelper$ServletServiceAction.run(StubSecurityHelper.java:227)][weblogic.servlet.internal.StubSecurityHelper.invokeServlet(StubSecurityHelper.java:125)][weblogic.servlet.internal.ServletStubImpl.execute(ServletStubImpl.java:292)][weblogic.servlet.internal.ServletStubImpl.execute(ServletStubImpl.java:175)][weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.run(WebAppServletContext.java:3498)][weblogic.security.acl.internal.AuthenticatedSubject.doAs(AuthenticatedSubject.java:321)][weblogic.security.service.SecurityManager.runAs(Unknown Source)][weblogic.servlet.internal.WebAppServletContext.securedExecute(WebAppServletContext.java:2180)][weblogic.servlet.internal.WebAppServletContext.execute(WebAppServletContext.java:2086)][weblogic.servlet.internal.ServletRequestImpl.run(ServletRequestImpl.java:1406)][weblogic.work.ExecuteThread.execute(ExecuteThread.java:201)][weblogic.work.ExecuteThread.run(ExecuteThread.java:173)]}
I want to extract the Val and SubVal from that string and my pattern is:
^(?:(?:.|\\n|\\r)*)Val=(\\d+);SubVal=(\\d+)(?:(?:.)*)$
I got this exception:
Exception in thread "main" java.lang.StackOverflowError
at java.lang.String.charAt(String.java:685)
at java.lang.Character.codePointAt(Character.java:2335)
at java.util.regex.Pattern$CharProperty.match(Pattern.java:3344)
at java.util.regex.Pattern$Branch.match(Pattern.java:4114)
at java.util.regex.Pattern$GroupHead.match(Pattern.java:4168)
at java.util.regex.Pattern$Loop.match(Pattern.java:4295)
at java.util.regex.Pattern$GroupTail.match(Pattern.java:4227)
at java.util.regex.Pattern$BranchConn.match(Pattern.java:4078)
at java.util.regex.Pattern$CharProperty.match(Pattern.java:3345)
at java.util.regex.Pattern$Branch.match(Pattern.java:4114)
at java.util.regex.Pattern$GroupHead.match(Pattern.java:4168)
at java.util.regex.Pattern$Loop.match(Pattern.java:4295)
at java.util.regex.Pattern$GroupTail.match(Pattern.java:4227)
How should I do to extract that Val and SubVal value from that string by using Java RegEx?
I see a lot of extraneous matches; why won't a simple [{]Val=(\\d+);SubVal=(\\d+);
work?
Java's RE engine has some problems with repetitions containing alternatives: They get compiled to a recursive function, and on long strings like your's, you get the StackOverflowError.
I'm not sure why you need the ^(?:(?:.|\\n|\\r)*)
at the start, and (?:(?:.)*)$
at the end - shouldn't this work without those, too, if you are using find
instead of matches
?
Here is some example code:
Pattern p = Pattern.compile("Val=(\\d+);SubVal=(\\d+);");
Matcher m = p.matcher(string);
if(m.find()) {
int val = Integer.parseInt(m.group(1));
int subVal = Integer.parseInt(m.group(2));
}
(Of course, if you do this several times, make the Pattern some static variable to avoid recompiling it for each use.)
精彩评论