2013-10-18 27 views
0

所以我一直在試圖做出這個簡單的加密程序,但我似乎無法弄清楚一些事情。我需要輸入短語是使用正則表達式替換子字符串

This is a very big morning. 

當我進入它,雖然它返回字符串

This is a ag',rery dug>?/ijeb..w ssadorninjeb..w 

相反,我回到

This is a ajedg>P/..w',rery dg>P/ijedg>P/..w ssadorninjedg>P/..w 

我不明白爲什麼,以及如何解決它?我一直在學習java一個月左右,所以我仍然很新鮮,如果有類似的問題已經得到解答,請在那裏與我聯繫,我將刪除這篇文章。

下面的代碼:

import static java.lang.System.out; 
import java.util.Scanner; 
class Encryption { 
    public static void main(String[] args) { 
     Scanner userInput = new Scanner(System.in); 
     Crypto user1 = new Crypto(); 
     out.print("Please enter in a sentence: "); 
     String user = userInput.nextLine(); 
     user1.encrypt(user); 
     out.print(user1.getEncrypt()); 
    } 
} 

public Crypto() { } 
public String myEn; 
public void encrypt(String Sentence) { 
    myEn = Sentence.replaceAll("v","ag',r") 
        .replaceAll("m" , "ssad") 
        .replaceAll("g" , "jeb..w") 
        .replaceAll("b" , "dg>P/"); 
} 

public String getEncrypt() { 
     return myEn; 
} 
} 

回答

0

當您更換g它的替代含有b這樣的話,當你更換b的你的所有b「從g更換S還更換。同時還爲v

你可以做的是

Sentence.replaceAll("g" , "jeb..w") 
    .replaceFirst("b" , "dg>P/")  // as no g's before b's and only want to replace the first 
    .replaceFirst("v","ag',r") 
    .replaceFirst("m" , "ssad"); 

但對於這一句這僅適用。


你可以做什麼:

  1. 在地圖上添加所有的字符將被替換,有更換。
  2. 創建要在原始字符串上替換的每個字符的索引列表。
  3. 反轉列表的順序(最高到最低)
  4. 從列表中的第一個索引開始(最後一個字符來代替)從地圖
  5. 重複4該指數與它的替代在替換的字符向後向下工作。
2

你得到不同輸出的原因是因爲鏈接的替換接受了以前替換的返回值。所以在你的情況下,如果有v,它將被改爲ag',r,其中有一個g。那然後會觸發replaceAll("g" , "jeb..w")

爲了避免這種情況發生,你應該改變的內容替換的順序:

Sentence.replaceAll("g" , "jeb..w") 
     .replaceAll("b" , "dg>P/") 
     .replaceAll("v","ag',r") 
     .replaceAll("m" , "ssad"); 

然而,前兩個替代語句不能固定的,因爲一個與具有在g一個字符串替換b它,反之亦然,所以你可能想改變你要替換的角色。

0

首先,您應該使用replace(),而不是replaceAll()。兩者都替換找到的所有匹配,但replaceAll()使用正則表達式匹配並替換()使用純文本。

接下來,您的替換正在相互影響,所以使用一個StringBuffer並從第八個工作並結束向後一次替換一個字符。

解密同樣應該從左側開始一次處理一個字符。

0

正如其他人已經告訴你的問題是,你在幾次迭代(replaceAll調用)而不是一個替換字符。如果要防止替換用於替換其他字符的字符,可使用Matcher類中的appendReplacementappendTail

這裏是你如何能做到像

// We need to store somewhere info about original and its replacement 
// so we will use Map 
Map<String, String> replacements = new HashMap<>(); 
replacements.put("v", "ag',r"); 
replacements.put("m", "ssad"); 
replacements.put("g", "jeb..w"); 
replacements.put("b", "dg>P/"); 

// we need to create pattern which will find characters we want to replace 
Pattern pattern = Pattern.compile("[vmgb]");//this will do 

Matcher matcher = pattern.matcher("This is a very big morning."); 

StringBuffer sb = new StringBuffer();// this will be used to create 
            // string with replaced characters 

// lets start replacing process 
while (matcher.find()) {//first we need to find our characters 

    // then pick from map its replacement 
    String replacement = replacements.get(matcher.group()); 
    // and pass it to appendReplacement method 
    matcher.appendReplacement(sb, replacement); 

    // we repeat this process until original string has no more 
    // characters to replace 
} 
//after all we need to append to StringBuffer part after last replacement 
matcher.appendTail(sb); 

System.out.println(sb); 

輸出:

This is a ag',rery dg>P/ijeb..w ssadorninjeb..w. 

瞧。