我想搜索一些东西,但返回它后面的所有内容,但不返回搜索字符串本身
例如“ 一些蹩脚的句子很棒 ”
搜索“ 句子 ”
返回“ 太棒了 ”
到目前为止这是我的正则表达式
sentence(.*)
但它返回:句子很棒
Pattern pattern = Pattern.compile("sentence(.*)");
Matcher matcher = pattern.matcher("some lame sentence that is awesome");
boolean found = false;
while (matcher.find())
{
System.out.println("I found the text: " + matcher.group().toString());
found = true;
}
if (!found)
{
System.out.println("I didn't find the text");
}
发布于 2018-07-25 17:39:57
你可以使用“正则表达式”来执行此操作:
(?<=sentence).*
发布于 2018-07-25 18:21:24
你的"sentence(.*)"
是对的。要检索括号中组的内容,可以调用:
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"
}
https://stackoverflow.com/questions/-100005753
复制相似问题