故障排除 Java replaceAll
Troubleshooting Java replaceAll
我正在尝试编写一种方法,该方法接受要查找的输入字符串和用于替换所找到单词的所有实例的输入字符串,并 return 进行替换的次数。我正在尝试使用 JAVA 正则表达式中的模式和匹配器。我有一个名为 "text.txt" 的文本文件,其中包含 "this is a test this is a test this is a test"。当我尝试搜索 "test" 并将其替换为 "mess" 时,方法 returns 1 each 和单词 test 的 none 被替换。
public int findAndRepV2(String word, String replace) throws FileNotFoundException, IOException
{
int cnt = 0;
BufferedReader input = new BufferedReader( new FileReader(this.filename));
Writer fw = new FileWriter("test.txt");
String line = input.readLine();
while (line != null)
{
Pattern pattern = Pattern.compile(word, Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(line);
while (matcher.find()) {matcher.replaceAll(replace); cnt++;}
line = input.readLine();
}
fw.close();
return cnt;
}
首先,您需要确保您搜索的文本不会被解释为正则表达式。你应该这样做:
Pattern pattern = Pattern.compile(Pattern.quote(word), Pattern.CASE_INSENSITIVE);
其次,replaceAll
做这样的事情:
public String replaceAll(String replacement) {
reset();
boolean result = find();
if (result) {
StringBuffer sb = new StringBuffer();
do {
appendReplacement(sb, replacement);
result = find();
} while (result);
appendTail(sb);
return sb.toString();
}
return text.toString();
}
注意它是如何调用 find
直到找不到任何东西的。这意味着您的循环只会 运行 一次,因为在第一次调用 replaceAll
之后,匹配器已经找到了所有内容。
您应该改用 appendReplacement
:
StringBuffer buffer = new StringBuffer();
while (matcher.find()) {
matcher.appendReplacement(buffer, replace);
cnt++;
}
buffer.append(line.substring(matcher.end()));
// "buffer" contains the string after the replacement
我注意到在你的方法中,你实际上并没有对替换后的字符串做任何事情。如果是这样的话,数一数有多少次find
returns true:
while (matcher.find()) {
cnt++;
}
我正在尝试编写一种方法,该方法接受要查找的输入字符串和用于替换所找到单词的所有实例的输入字符串,并 return 进行替换的次数。我正在尝试使用 JAVA 正则表达式中的模式和匹配器。我有一个名为 "text.txt" 的文本文件,其中包含 "this is a test this is a test this is a test"。当我尝试搜索 "test" 并将其替换为 "mess" 时,方法 returns 1 each 和单词 test 的 none 被替换。
public int findAndRepV2(String word, String replace) throws FileNotFoundException, IOException
{
int cnt = 0;
BufferedReader input = new BufferedReader( new FileReader(this.filename));
Writer fw = new FileWriter("test.txt");
String line = input.readLine();
while (line != null)
{
Pattern pattern = Pattern.compile(word, Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(line);
while (matcher.find()) {matcher.replaceAll(replace); cnt++;}
line = input.readLine();
}
fw.close();
return cnt;
}
首先,您需要确保您搜索的文本不会被解释为正则表达式。你应该这样做:
Pattern pattern = Pattern.compile(Pattern.quote(word), Pattern.CASE_INSENSITIVE);
其次,replaceAll
做这样的事情:
public String replaceAll(String replacement) {
reset();
boolean result = find();
if (result) {
StringBuffer sb = new StringBuffer();
do {
appendReplacement(sb, replacement);
result = find();
} while (result);
appendTail(sb);
return sb.toString();
}
return text.toString();
}
注意它是如何调用 find
直到找不到任何东西的。这意味着您的循环只会 运行 一次,因为在第一次调用 replaceAll
之后,匹配器已经找到了所有内容。
您应该改用 appendReplacement
:
StringBuffer buffer = new StringBuffer();
while (matcher.find()) {
matcher.appendReplacement(buffer, replace);
cnt++;
}
buffer.append(line.substring(matcher.end()));
// "buffer" contains the string after the replacement
我注意到在你的方法中,你实际上并没有对替换后的字符串做任何事情。如果是这样的话,数一数有多少次find
returns true:
while (matcher.find()) {
cnt++;
}