2014-02-19 107 views
0

我有一個文本,我需要提取兩個特定單詞之間的數據,例如活動:子活動:。這裏是我的文字:找到兩個特定單詞之間的字符串

Activity: S1. Outline Design 
Sub-Activity: S3.3 Walk through Release Backlog 
Question Tag: tag 
Questioner (role or team): Solution Architect 
Which response should the user read first?: Response 8 
Responder 1 (role or team): Developer 
Response 1: 
Responder 2 (role or team): Scrum Master 
Response 2: response2 
Responder 3 (role or team): Please select: 
Response 3: 
Responder 4 (role or team): Please select: 
Response 4: 
Responder 5 (role or team): Please select: 
Response 5: 

,我想出了這個代碼,但問題是,用於字這種模式,但只要我chaged它不工作了文本,任何人有任何想法:

private static String extractActivity(String text) { 
    Pattern pattern = Pattern.compile("(?:\\W|\\w)*Activity:(?:\\W)*(.*)(?:\\W)*Sub-Activity:(?:\\W|\\w)*", 
      Pattern.DOTALL); 
    Matcher matcher = pattern.matcher(text); 
    matcher.matches(); 
    String activities = matcher.group(1); 
    return activities; 
} 

它顯示我以下錯誤:

Feb 19, 2014 5:06:58 PM org.apache.catalina.core.StandardWrapperValve invoke 
SEVERE: Servlet.service() for servlet [default-dispatcher] in context with path [/webmi] threw exception [Request processing failed; nested exception is java.lang.IllegalStateException: No match found] with root cause 
java.lang.IllegalStateException: No match found 
    at java.util.regex.Matcher.group(Matcher.java:485) 
    at com.lloydsbanking.webmi.service.RSSReaderService.extractSubActivity(RSSReaderService.java:107) 
    at com.lloydsbanking.webmi.service.RSSReaderService.read(RSSReaderService.java:61) 
    at com.lloydsbanking.webmi.web.RssController.getFeed(RssController.java:34) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:606) 
    at org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:219) 
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132) 
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104) 
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:745) 
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:686) 
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80) 
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:925) 
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856) 
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:920) 
    at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:827) 
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:647) 
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:801) 
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:728) 
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305) 
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) 
    at org.tuckey.web.filters.urlrewrite.RuleChain.handleRewrite(RuleChain.java:176) 
    at org.tuckey.web.filters.urlrewrite.RuleChain.doRules(RuleChain.java:145) 
    at org.tuckey.web.filters.urlrewrite.UrlRewriter.processRequest(UrlRewriter.java:92) 
    at org.tuckey.web.filters.urlrewrite.UrlRewriteFilter.doFilter(UrlRewriteFilter.java:394) 
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243) 
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) 
    at com.lloydsbanking.webmi.web.VersionNumberFilter.doFilter(VersionNumberFilter.java:50) 
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243) 
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) 
    at com.opensymphony.sitemesh.webapp.SiteMeshFilter.obtainContent(SiteMeshFilter.java:129) 
    at com.opensymphony.sitemesh.webapp.SiteMeshFilter.doFilter(SiteMeshFilter.java:77) 
+0

如果更改文本,改變正則表達式匹配輸入和您的要求。 – devnull

+1

你需要提供一些你喜歡從這個輸入匹配的邏輯。 – anubhava

+0

我想閱讀這兩個詞之間的所有內容。 – user261002

回答

1

表達是有點過頭。對於整個字符串,也可以使用()而不是find()\\w匹配單詞字符,而\\W匹配非單詞字符。因此\\W|\\w可能是.

Pattern pattern = Pattern.compile("\\bActivity\\:(.*)\\bSub-Activity\\:", 
     Pattern.DOTALL); 
Matcher matcher = pattern.matcher(text); 
if (matcher.find()) { 
    String activities = matcher.group(1); 
    return activities; 
} 
throw new IllegalStateException("No activity in: " + text); 

我已經使用\\b用於在字邊界,匹配/消耗0個字符,並檢測字邊界。這也適用於文本開頭的Activity。 雖然我不確定,但我已經逃過冒號(:),但x:{2,3}會匹配xxxxx


正如@Pshemo所評論的,您的代碼原則上是正確的。有可能數據沒有到達。

變化

matcher.matches(); 

if (!matcher.matches)) { 
    throw new IllegalStateException("No activity in: " + text); 
} 
+0

這是很好的改進,但它似乎沒有解決異常,因爲正則表達式是用'(?:\\ W | \\ w)*'包圍的,如果至少有一個匹配,它將讓正則表達式匹配整個字符串。 – Pshemo

+0

這工作很好,非常感謝 – user261002

1

試試這個可以幫助你

public static void main(String[] args){ 
    String str = "Activity: S1. Outline Design Sub-Activity: S3.3 Walk through " + 
    "Release Backlog Question Tag: tag Questioner (role or team): Solution " + 
    "Architect Which response should the user read first?: Response 8 Responder" + 
    " 1 (role or team): Developer Response 1: Responder 2 (role or team): Scrum " + 
    "Master Response 2: response2 Responder 3 (role or team):"+ 
    "Please select: Response 3: Responder 4 (role or team): Please select: Response 4:" + 
    " Responder 5 (role or team): Please select: Response 5:"; 

    String regex = "(?<=Activity:).*?(?=Sub-Activity:)"; 

    Pattern pattern = Pattern.compile(regex); 
    Matcher matcher = pattern.matcher(str); 
    matcher.find(); 
    System.out.println(matcher.group()); 
} 
相關問題