Getting the text that follows after the regex match

后端 未结 5 1714
你的背包
你的背包 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(.*)")

提交回复
热议问题