2011-02-15 349 views
1

我正在使用第三方庫,它以String(非Exception對象)的形式返回異常信息。我想提取字符串堆棧跟蹤的某些部分以在GUI中顯示用戶。從Java中提取文本字符串

兩個例子:

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:

回答

2

此正則表達式將處理這兩個指標之間的一切......

原正則表達式:^Caused\sby:\s\S+:(.+)$

爲Java String:"^Caused\\sby:\\s\\S+:(.+)$"

你可以很容易地this online tool驗證此。

你需要包括多行選項

示例代碼片段:

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); 
} 
+0

WOW ......有一天,我需要學習如何使用正則表達式..這就像一個冠軍。 – systemoutprintln

1

使用string.indexOf獲取無效更改異常的位置,然後使用「at com。」的stirng.index。獲得堆棧跟蹤的下一部分的指標,tehn抓住調整爲異常文本字符串的長度作爲補償

1

用途:

StringUtils.substringsBetween(searchMe, start, end); 

像這樣:

String[] causes = StringUtils.substringsBetween(exceptionMessage, 
         "Caused by: com.sdf.configure.model.change.InvalidChangeException:", 
         "at com.sdf"); 

這會給你是一個數組由第二個和第三個參數分隔的文本。

這裏的 JavaDoc