尝试使用正则表达式拆分字符串,其中必须删除第一个和最后一个单词

问题描述 投票:0回答:1

我有一个字符串如下:-

string= "None I am using a new technology test"

我已经使用了 split [

string.split(" ")
],但无法在我的输出应该是的地方编写 reg ex

Str_output= "I am using a new technology"

基本上必须排除字符串的第一个和最后一个单词。

string.split(" ")[1]+string.split(" ")[2]+string.split(" ")[3]
等等

java regex string
1个回答
0
投票

这种和平的代码应该有效

String s= "None I am using a new technology test";
Pattern p = Pattern.compile("^\\w+\\s(.*)\\s\\w+$");
Matcher m = p.matcher(s);
m.find();
System.out.println(m.group(1));

^\\w+\\s
匹配第一个单词

\\s\\w+$
匹配最后一个单词

.*
剩下的

© www.soinside.com 2019 - 2024. All rights reserved.