我不确定您要做什么,但以防万一您想修改至少有四个字母的单词,您可以使用类似这样的东西(它会将带有 => 4 个字母的单词更改为其大写版本)
String data = "I need to purchase a new vehicle. I would prefer a BMW.";
Pattern patter = Pattern.compile("(?<![a-z\\-_'])[a-z\\-_']{4,}(?![a-z\\-_'])",
Pattern.CASE_INSENSITIVE);
Matcher matcher = patter.matcher(data);
StringBuffer sb = new StringBuffer();// holder of new version of our
// data
while (matcher.find()) {// lets find all words
// and change them with its upper case version
matcher.appendReplacement(sb, matcher.group().toUpperCase());
}
matcher.appendTail(sb);// lets not forget about part after last match
System.out.println(sb);
输出:
I NEED to PURCHASE a new VEHICLE. I WOULD PREFER a BMW.
或者,如果您将替换代码更改为类似
matcher.appendReplacement(sb, "["+matcher.group()+"]");
你会得到
I [need] to [purchase] a new [vehicle]. I [would] [prefer] a BMW.
现在你可以在每个[
和]
得到你想要的数组上拆分这样的字符串。