简体   繁体   English

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

[英]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: . 我有一个文本,我需要在两个特定的单词之间提取数据,例如在Activity: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. 而不是整个字符串的matches() ,可以为部件执行find() \\\\w matches a word character, and \\\\W a non-word character. \\\\w匹配单词字符, \\\\W表示非单词字符。 Hence \\\\W|\\\\w could be . 因此\\\\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);

I have used \\\\b for a word boundary, matching/consuming 0 characters, and detecting word boundaries. 我使用\\\\b作为单词边界,匹配/消耗0个字符,并检测单词边界。 This also works for Activity at the begin of text. 这也适用于文本开头的Activity I have escaped the colon ( : ) though I am unsure, but x:{2,3} would match xx or xxx . 我躲过了冒号( : )虽然我不能确定,但x:{2,3}将匹配xxxxx


As @Pshemo commented, your code is in principle correct. 正如@Pshemo评论的那样,您的代码原则上是正确的。 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());
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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