2014-05-18 9 views
5

Hy Anzeigeaufeinander folgende Duplikate Worte Entfernen von Text aus Regex verwenden und den neuen Text

Ich habe den folgenden Code:

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

Mit dem Ziel der aufeinander folgende Duplikate von Phrasen zu entfernen. Der Code funktioniert nur für eine Spalte mit Strings, die nicht für Phrasen in voller Länge gedacht sind.

Zum Beispiel sollte mein eingegeben werden:

Blah blah Hund, Katze, Maus. Katze Mäuse Hund Hund.

und die Ausgabe

Blah Hund, Katze, Maus. Katze-Mäuse-Hund.

Sincerly,

Antwort

19

Zunächst einmal die Regex [aA-zZ]* nicht tut, was Sie denken, es tut. Es bedeutet „paßt zu null oder mehr a s oder Zeichen im Bereich zwischen ASCII A und ASCII z oder Z s (die auch [, ], \ und andere umfassen)“. Es entspricht daher auch der leeren Zeichenfolge.

Unter der Annahme, dass Sie nur nach doppelten Wörtern suchen, die ausschließlich aus ASCII-Buchstaben besteht, Fall-unsensibel, das erste Wort zu halten (was bedeutet, dass Sie nicht "it's it's" oder "olé olé!" passen würde wollen), dann können Sie das tun in eine einzelne Regex Operation:

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

die

Hello hello Hello there there past pastures 

in

Hello there past pastures 
ändern

Erläuterung:

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

anzeigen live on regex101.com.

+0

Aber wie ich Ihre regex in meinem Programm verwenden. Ich habe eine Datei als Eingabe und möchte den Inhalt ohne Redundanz mit System.out.print anzeigen lassen. Danke dir :-) – SocketM

+0

Vielen Dank, aber was $ 1 bedeutet :-)? – SocketM

+0

@SocketM: Das ist eine spezielle Variable, die auf den Inhalt der ersten [einfangenden Gruppe] verweist (http://www.regular-expressions.info/brackets.html), in diesem Fall das erste Wort (das wir behalten wollen) . –

1

Darunter ist Ihr Code. Ich habe Zeilen benutzt, um Text und Tims regulären Ausdruck zu teilen.

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

} 

Viel Glück,

+0

Vielen Dank :-) – SocketM

0

Bellow Code funktionieren

Import java.util.Scanner;

importieren java.util.regex.Matcher;

importieren Sie java.util.regex.Muster;

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

}

Verwandte Themen