用特定单词替换字符串中的四个字母单词



我是字符串数组的新手,有一个关于用句子中的特定单词替换四个字母的单词的问题。例如,我得到一个字符串作为输入,如

Being good to them, always helps!

将四个字母的单词替换为单词love,例如它将是

Being love to love, always helps!

我如何将一个句子中所有四个字母的单词拆分,然后将其放入字符串数组中,完成其余的操作?

使用String.replaceAll和以下正则表达式查找4个字母的单词:"bw{4}b":

String sentence =  "Being good to them, always helps!";
String fixed = sentence.replaceAll("\b\w{4}\b", "love");
System.out.println(fixed);

输出

Being love to love, always helps!

这允许在不创建大量中间字符串及其数组的情况下获得结果字符串。


如果在拆分后真的需要这样的数组,替换这个数组中的单词,然后重建结果字符串,可以按如下方式完成。

此外,在下面的代码中,另一个字符类"p{L}{4}"用于匹配由仅字母组成的4字母单词,包括非英语字母。(上面使用的类w是数字、下划线和英文字母的快捷方式,[A-Za-z0-9_](

String sentence = sentence = "1234 is not a word.    Neither A_Z1. O'Neil in New-York. Орут коты.";
String[] parts = sentence.split("\b");
for (int i = 0; i < parts.length; i++) {
if (parts[i].matches("\p{L}{4}")) {
parts[i] = "love";
}
}
System.out.println(String.join("", parts));

输出(与4位数字相反,4位空格不被替换(:

1234 is not a love.    Neither A_Z1. O'love in New-love. love love.

此外,当可以从Pattern::splitAsStream获得Stream<String>并且使用Collectors.joining:获得结果时,可以使用Stream API实现结果的替换和检索

String str = Pattern.compile("\b").splitAsStream(sentence) // Stream<String>
.map(s -> s.matches("\w{4}") ? "love" : s)
.collect(Collectors.joining(""));

System.out.println(str);

输出(与非英语单词不同,带有数字的"单词"被替换(

love is not a love.    Neither love. O'love in New-love. Орут коты.

这里有一个程序可以满足您的要求:

String s = "I hate you, you doer"
var array = s.split("\b");
var list = new ArrayList<>(Arrays.asList(array));
while (list.remove(" ")) {};
List<String> result=new ArrayList<String>();
for (String word:list) 
if (word.length()==4) 
result.add("love"); 
else 
result.add(word);
System.out.println(String.join(" ", result).replace(" , ", ",")

输出为:

I love you, you love

以下是我的想法。我使用Java REPL是因为它使探索变得更容易。我忽略了逗号,假装它是一个单词的字母。如果你需要处理标点符号,事情会变得更混乱。

$ jshell
|  Welcome to JShell -- Version 11.0.10
|  For an introduction type: /help intro
jshell> String s = "I hate you, you doer"
s ==> "I hate you, you doer"
jshell> var array = s.split("\b");
array ==> String[9] { "I", " ", "hate", " ", "you", ", ", "you", " ", "doer" }
jshell> for (String word:array) if (word.length()==4) System.out.println(word);
...>
hate
doer
jshell> var list = new ArrayList<>(Arrays.asList(array));
list ==> [I,  , hate,  , you, , , you,  , doer]
while (list.remove(" ")) {};
jshell> list
list ==> [I, hate, you, , , you, doer]
jshell> List<String> result=new ArrayList<String>();
result ==> []
jshell> for (String word:list) if (word.length()==4) result.add("love"); else result.add(word);
...>
jshell> result
result ==> [I, love, you, , , you, love]
jshell> String.join(" ", result).replace(" , ", ",")
$17 ==> "I love you, you love"

您可以使用String#replaceAll方法:

String str1 = "Being good to them, always helps!";
// regex groups:
// (^|\W) - $1 - beginning of a string or a non-word character
// (w{4}) - $2 - sequence of 4 word characters
// (W|$)  - $3 - non-word character or end of a string
String str2 = str1.replaceAll("(^|\W)(\w{4})(\W|$)", "$1love$3");
System.out.println(str2);

输出:

Being love to love, always helps!

最新更新