我正在使用第三方库,它以String(不是Exception对象)的形式返回Exception信息。我想提取String堆栈跟踪的某些部分以在GUI中显示用户。
两个例子:
com.sdf.configure.model.change.InvalidChangeException: transformed change validation failed
at com.sdf.configure.model.change.SimpleConfigChanger.performChange(SimpleConfigChanger.java:129)
at com.sdf.portal.service.QosPolicyServiceImpl.applyPolicySnapshot(QosPolicyServiceImpl.java:99)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at com.referentia.commons.distributed.remoting.jms.JmsServiceExporter$1$1.run(JmsServiceExporter.java:189)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:441)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
at java.util.concurrent.FutureTask.run(FutureTask.java:138)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:619)
Caused by: com.sdf.configure.model.change.InvalidChangeException: can't add input service policy: policy has queueing
at com.sdf.configure.model.validate.RuleBasedValidator$RuleBasedChangeValidator.checkValidChange(RuleBasedValidator.java:5188)
at com.sdf.configure.model.validate.AbstractContextChangeValidator.checkValidChange(AbstractContextChangeValidator.java:304)
at com.sdf.configure.model.validate.AbstractContextChangeValidator.checkValidChange(AbstractContextChangeValidator.java:140)
at com.sdf.configure.model.validate.RuleBasedValidator.checkValidChange(RuleBasedValidator.java:1039)
at com.sdf.configure.model.change.SimpleConfigChanger.performChange(SimpleConfigChanger.java:126)
... 12 more
我想提取文字: 无法添加输入服务策略:策略已排队
com.sdf.configure.model.change.InvalidChangeException: transformed change validation failed
at com.sdf.configure.model.change.SimpleConfigChanger.performChange(SimpleConfigChanger.java:129)
at com.sdf.portal.service.QosPolicyServiceImpl.applyPolicySnapshot(QosPolicyServiceImpl.java:99)
at sun.reflect.GeneratedMethodAccessor139.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at com.sdf.commons.distributed.remoting.jms.JmsServiceExporter$1$1.run(JmsServiceExporter.java:189)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:441)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
at java.util.concurrent.FutureTask.run(FutureTask.java:138)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:619)
Caused by: com.sdf.configure.model.change.InvalidChangeException: compound change component validation failed
at com.sdf.configure.model.validate.AbstractContextChangeValidator.checkValidChange(AbstractContextChangeValidator.java:1309)
at com.sdf.configure.model.validate.AbstractContextChangeValidator.checkValidChange(AbstractContextChangeValidator.java:361)
at com.sdf.configure.model.validate.AbstractContextChangeValidator.checkValidChange(AbstractContextChangeValidator.java:140)
at com.sdf.configure.model.validate.RuleBasedValidator.checkValidChange(RuleBasedValidator.java:1039)
at com.sdf.configure.model.change.SimpleConfigChanger.performChange(SimpleConfigChanger.java:126)
... 11 more
Caused by: com.sdf.configure.model.change.InvalidChangeException: can't add input service policy: policy has shaping
at com.sdf.configure.model.validate.RuleBasedValidator$RuleBasedChangeValidator.checkValidChange(RuleBasedValidator.java:5180)
at com.sdf.configure.model.validate.AbstractContextChangeValidator.checkValidChange(AbstractContextChangeValidator.java:304)
at com.sdf.configure.model.validate.AbstractContextChangeValidator.checkValidChange(AbstractContextChangeValidator.java:1306)
... 15 more
我想提取文字:
化合物更改组件验证失败
无法添加输入服务政策:政策已整形
任何优雅的方式来做到这一点?找到我想要提取的部分的唯一方法似乎是我要提取的部分总是先于:
引起:com.sdf.configure.model.change.InvalidChangeException:
答案 0 :(得分:2)
这个正则表达式将处理......
原始正则表达式:^Caused\sby:\s\S+:(.+)$
作为Java字符串:"^Caused\\sby:\\s\\S+:(.+)$"
您可以使用this online tool轻松验证这一点。
您需要包含MULTILINE选项
示例代码snippit:
String regex = "^Caused\\sby:\\s\\S+:(.+)$";
Pattern p = Pattern.compile(regex, Pattern.MULTILINE);
Matcher m = p.matcher(exceptionString);
while (m.find()) {
String outputThis = m.group(1);
}
答案 1 :(得分:1)
使用string.indexOf获取无效更改异常的位置,然后使用“at com”的stirng.index。为了获得堆栈跟踪的下一部分的索引,tehn抓取两个索引之间的所有内容,将异常文本字符串长度调整为偏移量
答案 2 :(得分:1)
使用:
StringUtils.substringsBetween(searchMe, start, end);
像这样:
String[] causes = StringUtils.substringsBetween(exceptionMessage,
"Caused by: com.sdf.configure.model.change.InvalidChangeException:",
"at com.sdf");
这将为您提供由第2和第3个参数分隔的文本数组。
这是 JavaDoc