2014-05-18 12 views
5

Hỷ,Loại bỏ từ bản sao liên tiếp ra khỏi văn bản sử dụng Regex và hiển thị các văn bản mới

Tôi có đoạn mã sau:

import java.io.*; 
import java.util.ArrayList; 
import java.util.Scanner; 
import java.util.regex.*; 

/
public class RegexSimple4 
{ 

    public static void main(String[] args) { 

      try 
      { 
     Scanner myfis = new Scanner(new File("D:\\myfis32.txt")); 
      ArrayList <String> foundaz = new ArrayList<String>(); 
      ArrayList <String> noduplicates = new ArrayList<String>(); 

     while(myfis.hasNext()) 
     { 
      String line = myfis.nextLine(); 
      String delim = " "; 
      String [] words = line.split(delim); 



    for (String s : words) {      
        if (!s.isEmpty() && s != null) 
        { 
         Pattern pi = Pattern.compile("[aA-zZ]*"); 
         Matcher ma = pi.matcher(s); 

         if (ma.find()) { 
          foundaz.add(s); 
         } 
        } 
       } 
      } 
        if(foundaz.isEmpty()) 
       { 
        System.out.println("No words have been found"); 
       } 

        if(!foundaz.isEmpty()) 
        { 
         int n = foundaz.size(); 
         String plus = foundaz.get(0); 
         noduplicates.add(plus); 
         for(int i=1; i<n; i++) 
         { 
          if(!noduplicates.get(i-1).equalsIgnoreCase(foundaz.get(i))) 
          { 
          noduplicates.add(foundaz.get(i)); 
          } 
         } 
         //System.out.print("Cuvantul/cuvintele \n"+i); 

       } 
        if(!foundaz.isEmpty()) 
        { System.out.print("Original text \n"); 
         for(String s: foundaz) 
         { 
          System.out.println(s); 
       } 
         } 
        if(!noduplicates.isEmpty()) 
        { System.out.print("Remove duplicates\n"); 
         for(String s: noduplicates) 
         { 
          System.out.println(s); 
       } 
         } 

     } 


catch(Exception ex) 
    { 
     System.out.println(ex); 
    } 
} 
} 

Với mục đích loại bỏ các bản sao liên tiếp từ cụm từ. Mã chỉ hoạt động cho một cột các chuỗi không cho các cụm từ có độ dài đầy đủ.

Ví dụ đầu vào của tôi nên là:

Blah blah chuột chó mèo. Chó mèo chó chó.

Và đầu ra

chuột Blah chó mèo. Chó con chó mèo.

Sincerly,

Trả lời

19

Trước hết, regex [aA-zZ]* không làm những gì bạn nghĩ rằng nó. Nó có nghĩa là "Phù hợp với zero hoặc nhiều a s hoặc các ký tự trong phạm vi giữa ASCII A và ASCII z (mà cũng bao gồm [, ], \ và những người khác), hoặc Z s". Do đó, nó cũng phù hợp với chuỗi rỗng.

Giả sử bạn chỉ tìm kiếm các từ trùng lặp bao gồm chữ cái ASCII, không phân biệt chữ hoa chữ thường, giữ từ đầu tiên (có nghĩa là bạn không muốn khớp với "it's it's" hoặc "olé olé!"), thì bạn có thể làm điều đó một hoạt động regex duy nhất:

String result = subject.replaceAll("(?i)\\b([a-z]+)\\b(?:\\s+\\1\\b)+", "$1"); 

mà sẽ thay đổi

Hello hello Hello there there past pastures 

vào

Hello there past pastures 

Giải thích:

(?i)  # Mode: case-insensitive 
\b  # Match the start of a word 
([a-z]+) # Match one ASCII "word", capture it in group 1 
\b  # Match the end of a word 
(?:  # Start of non-capturing group: 
\s+  # Match at least one whitespace character 
\1  # Match the same word as captured before (case-insensitively) 
\b  # and make sure it ends there. 
)+  # Repeat that as often as possible 

Xem nó live on regex101.com.

+0

Nhưng làm thế nào tôi sử dụng regex của bạn trong chương trình của tôi. Tôi có một tập tin như là một đầu vào và tôi muốn hiển thị nội dung của nó mà không cần dự phòng bằng cách sử dụng System.out.print. Cảm ơn u :-) – SocketM

+0

Cảm ơn bạn rất nhiều, nhưng những gì $ 1 có nghĩa là :-)? – SocketM

+0

@SocketM: Đó là một biến đặc biệt tham chiếu đến nội dung của [nhóm bắt giữ] đầu tiên (http://www.regular-expressions.info/brackets.html), trong trường hợp này là từ đầu tiên (chúng tôi muốn giữ) . –

1

Dưới đây là mã của bạn. Tôi đã sử dụng các dòng để phân tách văn bản và biểu thức chính quy của Tim.

import java.util.Scanner; 
import java.io.*; 
import java.util.regex.*; 
import java.util.ArrayList; 
/** 
* 
* @author Marius 
*/ 
public class RegexSimple41 { 

    /** 
    * @param args the command line arguments 
    */ 
    public static void main(String[] args) { 
     ArrayList <String> manyLines = new ArrayList<String>(); 
     ArrayList <String> noRepeat = new ArrayList<String>(); 
     try 
     { 
      Scanner myfis = new Scanner(new File("D:\\myfis41.txt")); 

      while(myfis.hasNext()) 
      { 
       String line = myfis.nextLine(); 
       String delim = System.getProperty("line.separator"); 
       String [] lines = line.split(delim); 

       for(String s: lines) 
       { 
        if(!s.isEmpty()&&s!=null) 
        { 
         manyLines.add(s); 
        } 
       } 
      } 
      if(!manyLines.isEmpty()) 
        { System.out.print("Original text\n"); 
         for(String s: manyLines) 
         { 
          System.out.println(s); 
       } 
         } 
      if(!manyLines.isEmpty()) 
        { 
         for(String s: manyLines) 
         { 
          String result = s.replaceAll("(?i)\\b([a-z]+)\\b(?:\\s+\\1\\b)+", "$1"); 
          noRepeat.add(result); 
       } 
         } 
      if(!noRepeat.isEmpty()) 
        { System.out.print("Remove duplicates\n"); 
         for(String s: noRepeat) 
         { 
          System.out.println(s); 
       } 
         } 

     } 

     catch(Exception ex) 
     { 
      System.out.println(ex); 
     } 
    } 

} 

Chúc may mắn,

+0

Cảm ơn bạn rất nhiều :-) – SocketM

0

Bellow đang làm việc tốt java.util.Scanner

nhập khẩu;

nhập java.util.regex.Matcher;

nhập java.util.regex.Mẫu;

public class DuplicateRemoveEx {

public static void main(String[] args){ 

    String regex="(?i)\\b(\\w+)(\\b\\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); 
     while(m.find()){ 
      input=input.replaceAll(regex, "$1"); 
     } 
     System.out.println(input); 
    } 
    in.close(); 
} 

}

Các vấn đề liên quan