简体   繁体   中英

Find a string between two specific word

I have a text and I need to extract a data between two specific words for example between Activity: and Sub-Activity: . here is my text :

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: 

and I came up with this code, but the problem is that this pattern used to word, but as soon as I chaged the text it doesnt work any more, anybody has any idea:

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;
}

it shows me the following error :

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)

The expression is a bit overdone. Also instead of a matches() for the entire string, one can do a find() for a part. \\\\w matches a word character, and \\\\W a non-word character. Hence \\\\W|\\\\w could be . .

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);

I have used \\\\b for a word boundary, matching/consuming 0 characters, and detecting word boundaries. This also works for Activity at the begin of text. I have escaped the colon ( : ) though I am unsure, but x:{2,3} would match xx or xxx .


As @Pshemo commented, your code is in principle correct. Likely the data does not arrive as attended.

Change

matcher.matches();

to

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

Try this may help you

 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());
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM