2
我使用的是input = input.replaceAll(regex, "$1");
一起"\\b(\\w+)(\\W+\\1\\b)+"
找到字符串中的重複單詞,並刪除重複的。例如,字符串input =「for for for」將變爲「for」。Java的正則表達式(我)對Pattern.CASE_INSENSITIVE
但是它沒有把「你好你好」到「你好」即使我已經用"(?i)\\b(\\w+)(\\W+\\1\\b)+"
使用Pattern p = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
我可以糾正它,但我想知道這是爲什麼有必要嗎?當我已經指定Pattern.CASE_INSENSITIVE時,爲什麼我必須使用(?i)標誌?
繼承人爲清晰起見,全碼:
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class DuplicateWords {
public static void main(String[] args) {
String regex = "\\b(\\w+)(\\W+\\1\\b)+";
Pattern p = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
Scanner in = new Scanner(System.in);
int numSentences = Integer.parseInt(in.nextLine());
while (numSentences-- > 0) {
String input = in.nextLine();
Matcher m = p.matcher(input);
// Check for subsequences of input that match the compiled pattern
while (m.find()) {
input = input.replaceAll(regex, "$1");
}
// Prints the modified sentence.
System.out.println(input);
}
in.close();
}
}
因此,只有立即之後的效果塊(在這種情況下\\ 1),而不是整個正則表達式? – Paddy
是的,這是正確的 – anubhava
我還是不明白,爲什麼我必須使用(我)都沒有。 Should'nt Pattern.CASE_INSENSITIVE是否足夠? – Paddy