Getting the text that follows after the regex match

后端 未结 5 1653
你的背包
你的背包 2020-11-22 11:33

I\'m new to using Regex, I\'ve been going through a rake of tutorials but I haven\'t found one that applies to what I want to do,

I want to search for something, but

相关标签:
5条回答
  • 2020-11-22 11:55

    Your regex "sentence(.*)" is right. To retrieve the contents of the group in parenthesis, you would call:

    Pattern p = Pattern.compile( "sentence(.*)" );
    Matcher m = p.matcher( "some lame sentence that is awesome" );
    if ( m.find() ) {
       String s = m.group(1); // " that is awesome"
    }
    

    Note the use of m.find() in this case (attempts to find anywhere on the string) and not m.matches() (would fail because of the prefix "some lame"; in this case the regex would need to be ".*sentence(.*)")

    0 讨论(0)
  • 2020-11-22 12:03

    You can do this with "just the regular expression" as you asked for in a comment:

    (?<=sentence).*
    

    (?<=sentence) is a positive lookbehind assertion. This matches at a certain position in the string, namely at a position right after the text sentence without making that text itself part of the match. Consequently, (?<=sentence).* will match any text after sentence.

    This is quite a nice feature of regex. However, in Java this will only work for finite-length subexpressions, i. e. (?<=sentence|word|(foo){1,4}) is legal, but (?<=sentence\s*) isn't.

    0 讨论(0)
  • 2020-11-22 12:09

    You just need to put "group(1)" instead of "group()" in the following line and the return will be the one you expected:

    System.out.println("I found the text: " + matcher.group(**1**).toString());
    
    0 讨论(0)
  • 2020-11-22 12:13

    if Matcher is initialized with str, after the match, you can get the part after the match with

    str.substring(matcher.end())
    

    Sample Code:

    final String str = "Some lame sentence that is awesome";
    final Matcher matcher = Pattern.compile("sentence").matcher(str);
    if(matcher.find()){
        System.out.println(str.substring(matcher.end()).trim());
    }
    

    Output:

    that is awesome

    0 讨论(0)
  • 2020-11-22 12:14

    You need to use the group(int) of your matcher - group(0) is the entire match, and group(1) is the first group you marked. In the example you specify, group(1) is what comes after "sentence".

    0 讨论(0)
提交回复
热议问题