我有一個文本,我需要提取兩個特定單詞之間的數據,例如活動:和子活動:。這裏是我的文字:找到兩個特定單詞之間的字符串
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)
如果更改文本,改變正則表達式匹配輸入和您的要求。 – devnull
你需要提供一些你喜歡從這個輸入匹配的邏輯。 – anubhava
我想閱讀這兩個詞之間的所有內容。 – user261002