2013-05-22 15 views
7

ho trovatoSostituzione molteplici occorrenze di caratteri

String seq = "123456789"; 
    String regex = seq.replaceAll(".", "(?=[$0-9]([a-z]))?") + "[0-9][a-z]"; 
    String repl = seq.replaceAll(".", "\\$$0"); 

che si trasforma in 4a aaaa, 3b in BBB e così via ... bisogno l'opposto e non ho potuto capire. Devo trasformare aaaa in 4a, bbb in 3b e così via. Grazie mille

+1

Escape '.' come questo' \\. '. Si noti che 'replaceAll' richiede una regex ** e non una String. ('.' ha un significato speciale in regex) – Maroun

+0

String file = new Scanner (new File (" Jutt.txt ")). useDelimiter (" \\ A "). next(); \t \t String seq = "123456789"; \t \t String regex = seq.replaceAll ("\\.", "(? = [$ 0-9] ([a-z]))?") + "[0-9] [a-z]"; \t \t String repl = seq.replaceAll ("\\.", "\\ $$ 0"); \t \t \t \t file = file.replaceAll (repl, regex); Parte un po 'più lunga del codice. Ho bisogno di copiare il testo da 1 file a un altro, usando alcune modifiche, come detto prima "Devo trasformare aaaa in 4a, bbb in 3b e così via". Al momento sta facendo il contrario :( – user2408677

+0

Lui usa la stringa '1..9' per generare un'espressione regolare:' (? = [1-9] ([az]))? (? = [2-9] ([az]))? (? = [3-9] ([az]))? (? = [4-9] ([az]))? (? = [5-9] ([az])) ? (? = [6-9] ([az]))? (? = [7-9] ([az]))? (? = [8-9] ([az]))? (? = [ 9-9] ([az]))? [0-9] [az] 'e usa quello con la sostituzione' $ 1 $ 2 $ 3 $ 4 $ 5 $ 6 $ 7 $ 8 $ 9' per trasformare '4a' in' aaaa'. Funziona per ' [0-9] [az] '. Mi sembra come qualcosa che potresti non voler fare con regex però ... – rvalvik

risposta

1

Ecco un esempio di una codifica run-length/decodifica implementazione in Java:

import java.util.regex.Matcher; 
import java.util.regex.Pattern; 
public class RunLengthEncoding { 

    public static String encode(String source) { 
     StringBuffer dest = new StringBuffer(); 
     for (int i = 0; i < source.length(); i++) { 
      int runLength = 1; 
      while (i+1 < source.length() && source.charAt(i) == source.charAt(i+1)) { 
       runLength++; 
       i++; 
      } 
      dest.append(runLength); 
      dest.append(source.charAt(i)); 
     } 
     return dest.toString(); 
    } 

    public static String decode(String source) { 
     StringBuffer dest = new StringBuffer(); 
     Pattern pattern = Pattern.compile("[0-9]+|[a-zA-Z]"); 
     Matcher matcher = pattern.matcher(source); 
     while (matcher.find()) { 
      int number = Integer.parseInt(matcher.group()); 
      matcher.find(); 
      while (number-- != 0) { 
       dest.append(matcher.group()); 
      } 
     } 
     return dest.toString(); 
    } 

    public static void main(String[] args) { 
     String example = "WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWBWWWWWWWWWWWWWW"; 
     System.out.println(encode(example)); 
     System.out.println(decode("1W1B1W1B1W1B1W1B1W1B1W1B1W1B")); 
    } 
} 

Tratto da qui:

http://rosettacode.org/wiki/Run-length_encoding

(questa pagina include esempi equivalenti a 72 diversi linguaggi di programmazione per raggiungere lo stesso obiettivo)

Per ottenere quello che stai chiedendo, dovresti usare "enco" de "metodo.

provato qui: http://www.browxy.com/SubmittedCode/21369

Regex di per sé non è uno strumento adatto per cercare di raggiungere questo obiettivo.

Problemi correlati