仅当它是一个独立的词时,如何替换关键短语?
How do I replace a key phrase only if it is a standalone word?
例如,如果我要制作关键词 "ball",那么我需要句子 "He caught the ball because he was playing baseball" 到 "He caught the XXXX because he was playing baseball"。相反,我得到 "He caught the XXXX because he was playing baseXXXX"。我正在使用 replaceAll,这就是我收到此错误的原因,但我想知道是否有一种方法可以让我检查之后是否有一个不是空格的字符,并且只有在有空格时才替换这个词或特殊字符。我挣扎的原因是因为如果后面跟着一个特殊字符而不是字母,我仍然希望替换这个词。
您可以使用 boundary matcher(注意您必须转义反斜杠)。单词边界 \b
也处理标点符号:
System.out.println("He caught the ball because he was playing baseball"
.replaceAll("\bball\b", "XXXX"));
System.out.println("One ball, two cats. Two cats with a ball."
.replaceAll("\bball\b", "XXXX"));
System.out.println("A ball? A ball!".replaceAll("\bball\b", "XXXX"));
打印:
He caught the XXXX because he was playing baseball
One XXXX, two cats. Two cats with a XXXX.
A XXXX? A XXXX!
试试这个:
String s = "He caught the ball because he was playing baseball";
String replaceKey = "ball";
String replaceValue = "XXXX";
String[] sArray = s.split(" ");
String finalS = "";
for(int i=0; i<sArray.length;i++) {
if(replaceKey.equals(sArray[i])){
sArray[i] = replaceValue;
}
}
System.out.println(String.join(" ", sArray));
输出:He caught the XXXX because he was playing baseball
编辑:
String s = "!ball! ?ball1 ball! ball1 ball, 1ball1 baseball ba.ll";
String replaceKeyPattern = "^[\W+[ball]\W+]+$";
/* \W is all non word characters= special characters */
String replaceKey = "ball";
String replaceValue = "XXXX";
String[] sArray = s.split(" ");
String finalS = "";
for(int i=0; i<sArray.length;i++) {
if(sArray[i].matches(replaceKeyPattern)){
sArray[i] = sArray[i].replace(replaceKey,replaceValue);
}
}
System.out.println(String.join(" ", sArray));
输出:!XXXX! ?ball1 XXXX! ball1 XXXX, 1ball1 baseball ba.ll
希望对您有所帮助!
例如,如果我要制作关键词 "ball",那么我需要句子 "He caught the ball because he was playing baseball" 到 "He caught the XXXX because he was playing baseball"。相反,我得到 "He caught the XXXX because he was playing baseXXXX"。我正在使用 replaceAll,这就是我收到此错误的原因,但我想知道是否有一种方法可以让我检查之后是否有一个不是空格的字符,并且只有在有空格时才替换这个词或特殊字符。我挣扎的原因是因为如果后面跟着一个特殊字符而不是字母,我仍然希望替换这个词。
您可以使用 boundary matcher(注意您必须转义反斜杠)。单词边界 \b
也处理标点符号:
System.out.println("He caught the ball because he was playing baseball"
.replaceAll("\bball\b", "XXXX"));
System.out.println("One ball, two cats. Two cats with a ball."
.replaceAll("\bball\b", "XXXX"));
System.out.println("A ball? A ball!".replaceAll("\bball\b", "XXXX"));
打印:
He caught the XXXX because he was playing baseball
One XXXX, two cats. Two cats with a XXXX.
A XXXX? A XXXX!
试试这个:
String s = "He caught the ball because he was playing baseball";
String replaceKey = "ball";
String replaceValue = "XXXX";
String[] sArray = s.split(" ");
String finalS = "";
for(int i=0; i<sArray.length;i++) {
if(replaceKey.equals(sArray[i])){
sArray[i] = replaceValue;
}
}
System.out.println(String.join(" ", sArray));
输出:He caught the XXXX because he was playing baseball
编辑:
String s = "!ball! ?ball1 ball! ball1 ball, 1ball1 baseball ba.ll";
String replaceKeyPattern = "^[\W+[ball]\W+]+$";
/* \W is all non word characters= special characters */
String replaceKey = "ball";
String replaceValue = "XXXX";
String[] sArray = s.split(" ");
String finalS = "";
for(int i=0; i<sArray.length;i++) {
if(sArray[i].matches(replaceKeyPattern)){
sArray[i] = sArray[i].replace(replaceKey,replaceValue);
}
}
System.out.println(String.join(" ", sArray));
输出:!XXXX! ?ball1 XXXX! ball1 XXXX, 1ball1 baseball ba.ll
希望对您有所帮助!