找到两个特定单词之间的字符串

时间:2014-02-19 16:59:03

标签: java regex string pattern-matching

我有一个文字,我需要在两个特定单词之间提取数据,例如在活动:子活动:之间。这是我的文字:

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: 

我提出了这个代码,但问题是这种模式习惯于说话,但是一旦我查阅了文本就不再有用了,任何人都有任何想法:

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)

2 个答案:

答案 0 :(得分:1)

表达有点过头了。也可以为整个字符串代替 matches(),而不是为部件执行 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);
}

答案 1 :(得分: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());
}