2014-05-18 23 views
5

Hy,Usuwanie duplikatów kolejnych słów z tekstu przy użyciu regex i wyświetlanie nowy tekst

Mam następujący kod:

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); 
    } 
} 
} 

w celu usunięcia duplikaty z fraz. Kod działa tylko dla kolumny ciągów, a nie dla pełnych fraz.

Na przykład mój wkład powinien być:

bla bla myszy pies kota. Cat mice dog dog.

i myszy Blah pies kot wyjście

. Kot myszy psa.

pozdrawiam Kaska

Odpowiedz

19

Przede wszystkim regex [aA-zZ]* nie robi tego, co myślisz, że to robi. To oznacza „zero lub więcej a s lub znaki w przedziale między ASCII A i ASCII z (która obejmuje również [, ], \ i inne) lub Z S”. Dlatego też pasuje do pustego łańcucha.

Zakładając, że szukasz tylko duplikatów słów, które składa się wyłącznie z liter ASCII, rozróżniana wielkość liter, utrzymując pierwsze słowo (co oznacza, że ​​nie chcesz, aby dopasować "it's it's" lub "olé olé!"), to można to zrobić w pojedyncza operacja regex:

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

który zmieni

Hello hello Hello there there past pastures 

do

Hello there past pastures 

Objaśnienie:

(?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 

Zobacz live on regex101.com.

+0

Ale jak mogę skorzystać z wyrażenia regularnego w moim programie. Mam plik jako dane wejściowe i chciałbym wyświetlić jego zawartość bez nadmiarowości za pomocą System.out.print. Dziękuję :-) – SocketM

+0

Dziękuję bardzo, ale co to 1 $ oznacza :-)? – SocketM

+0

@SocketM: Jest to specjalna zmienna odnosząca się do zawartości pierwszej [grupy przechwytującej] (http://www.regular-expressions.info/brackets.html), w tym przypadku pierwsze słowo (które chcemy zachować) . –

1

Poniżej znajduje się twój kod. Użyłem linii do podziału tekstu i wyrażenia regularnego Tima.

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); 
     } 
    } 

} 

Powodzenia

+0

Dziękuję bardzo :-) – SocketM

0

kod Bellow praca grzywny

import java.util.Scanner;

import java.util.regex.Matcher;

import java.util.regex.Wzór;

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(); 
} 

}

Powiązane problemy