2017-01-04 15 views
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

您的問題是你定義一個正則表達式與CASE_SENSITIVE標誌,但不是在replaceAll方法正確地使用它。

您還可以使用(?i)在正則表達式的中間爲忽略的反向引用\1情況下比賽是這樣的:

String repl = "Hello hello".replaceAll("\\b(\\w+)(\\W+(?i:\\1)\\b)+", "$1"); 
//=> Hello 

然後用Matcher.replaceAll以後。

工作代碼:(我)

public class DuplicateWords { 

    public static void main(String[] args) { 

     String regex = "\\b(\\w+)(\\W+(?i:\\1)\\b)+"; 
     Pattern p = Pattern.compile(regex); 

     // OR this one also works 
     // 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 
      if (m.find()) { 
       input = m.replaceAll("$1"); 
      } 

      // Prints the modified sentence. 
      System.out.println(input); 
     } 
     in.close(); 
    } 
} 
+0

因此,只有立即之後的效果塊(在這種情況下\\ 1),而不是整個正則表達式? – Paddy

+0

是的,這是正確的 – anubhava

+0

我還是不明白,爲什麼我必須使用(我)都沒有。 Should'nt Pattern.CASE_INSENSITIVE是否足夠? – Paddy