2015-06-11 53 views
0

這是我在這個最大社區的第一個問題,我從中學到了很多東西。使用正則表達式格式化值和多個零

我的要求是這樣的:
假設用戶輸入EK1,那麼輸出應該是EK1 EK01 EK001 EK0001
如果輸入是EK23,則輸出:EK23 EK023 EK0023
如果輸入是EK876,輸出:
如果輸入是EK0002,輸出:EK0002
如果輸入是MUSDG EK768 tygft然後輸出應爲MUSDG EK768 EK0768 tygft

這意味着無論用戶輸入什麼作爲輸入,輸出應該匹配2個字母和至少一個數字。
然後它應該開始加零。
如果它不匹配,那麼它應該返回與輸入相同的值。

我試過下面的代碼,但它在一個點上失敗。
如果數字只是一個例如EK1,則它給出EK1 EK01 EK001
但它應該給EK1 EK01 EK001 EK0001
即當前輸出缺少最後一個組合。

請參考下面的代碼,看看我是否做錯了。
請修改下面的代碼或給我建議。您的循環中

private static final String getPaddedValue(String toTest) { 
    StringBuilder paddedvalue = new StringBuilder(); 
    int zeroCounter = countOfAvailableZeroInToken(toTest); 
    for (int i = 0; i < 3 - zeroCounter; i++) { 
     StringBuilder currValue = new StringBuilder(); 
     currValue.append(toTest.substring(0, 2)); 
     for(int j = 0; j <= i; j++){ 
      currValue.append("0"); 
     } 
     currValue.append(toTest.split("[a-zA-Z]{2}")[1]).append(" "); 

     paddedvalue.append(currValue.toString()); 

     if(currValue.length() > 6) { 
      break; 
     } 
    } 
    return paddedvalue.toString(); 
} 

使用一個單獨的StringBuilder,並用它來檢查:

package com.abc.common.customersearch.repository.test; 
import java.util.regex.Matcher; 
import java.util.regex.Pattern; 

public class Test1 { 
    private static final Pattern zeroPattern = Pattern.compile("[0]"); 
    private static final String TOKEN_PATTERN = "[a-zA-Z]{2}[0-9]"; 
    private static final String ONE = "{1}"; 
    private static final String TWO = "{2}"; 
    private static final String THREE = "{3}"; 
    private static final String FOUR = "{4}"; 

    public static void main(String as[]) { 

     String inputToken = null; 
     String outputStr = null; 

     inputToken ="EK001"; 
     outputStr = flightToken(inputToken); 
     System.out.println("["+inputToken+"] >> ["+outputStr+"]"); 

     inputToken = "MUS EK874"; 
     outputStr = flightToken(inputToken); 
     System.out.println("["+inputToken+"] >> ["+outputStr+"]"); 

     inputToken ="EK08"; 
     outputStr = flightToken(inputToken); 
     System.out.println("["+inputToken+"] >> ["+outputStr+"]"); 

     inputToken ="MUS EK1"; // This is failing 
     outputStr = flightToken(inputToken); 
     System.out.println("["+inputToken+"] >> ["+outputStr+"]"); 

     inputToken ="MUS EK47"; 
     outputStr = flightToken(inputToken); 
     System.out.println("["+inputToken+"] >> ["+outputStr+"]"); 

     inputToken ="EK0001"; 
     outputStr = flightToken(inputToken); 
     System.out.println("["+inputToken+"] >> ["+outputStr+"]"); 

     inputToken ="E asfddsfsf EF549 dsgffdgfdgfdg"; 
     outputStr = flightToken(inputToken); 
     System.out.println("["+inputToken+"] >> ["+outputStr+"]"); 

     inputToken ="AB09"; 
     outputStr = flightToken(inputToken); 
     System.out.println("["+inputToken+"] >> ["+outputStr+"]"); 

     inputToken ="EK7 765kj 34h"; 
     outputStr = flightToken(inputToken); 
     System.out.println("["+inputToken+"] >> ["+outputStr+"]"); 



    } 

    public static String flightToken(String inputToken){ 

     StringBuilder finalToken = new StringBuilder(); 
     boolean isValidToken = false; 
     for (String token : inputToken.split("\\s")) { 
      if (isValidToken(token)) { 
       finalToken.append(token).append(" "); 
       finalToken.append(getPaddedValue(token)).append(" "); 
       isValidToken = true; 
      } else { 
       finalToken.append(token).append(" "); 
      } 
     } 
     if (isValidToken) { 
      //System.out.println("parsed token : " + finalToken.toString()); 
      return finalToken.toString(); 
     } else { 
      //System.out.println("invalid token : " + inputToken); 
      return inputToken; 
     } 

    } 


    private static final String getPaddedValue(String toTest) { 
     StringBuilder paddedvalue = new StringBuilder(); 
     int zeroCounter = countOfAvailableZeroInToken(toTest); 
     for (int i = 0; i < 3 - zeroCounter; i++) { 
      paddedvalue.append(toTest.substring(0, 2)); 
      for(int j= 0; j<=i; j++){ 
       paddedvalue.append("0"); 
      } 
      paddedvalue.append(toTest.split("[a-zA-Z]{2}")[1]).append(" "); 
      if(paddedvalue.length()-1>=6){ 
       break; 
      } 
     } 
     return paddedvalue.toString(); 
    } 


    private static final boolean isValidToken(String pattern) { 
     if (pattern.matches(TOKEN_PATTERN + FOUR) 
       || pattern.matches(TOKEN_PATTERN + THREE) 
       || pattern.matches(TOKEN_PATTERN + TWO) 
       || pattern.matches(TOKEN_PATTERN + ONE)) { 
      return true; 
     } else { 
      return false; 
     } 
    } 

    private static final int countOfAvailableZeroInToken(String toTest) { 
     int count = 0; 
     Matcher zeroMatcher = zeroPattern.matcher(toTest); 
     while (zeroMatcher.find()) { 
      ++count; 
     } 
     return count; 
    } 
} 
+0

「如果數字是隻有一個如EK1,那麼它給'EK1 EK01 EK001',但它應該給'EK1 EK01 EK001'「有什麼區別? – Gosu

+0

對不起。我編輯過它。它應該是EK1 EK01 EK001 EK0001。但是僅限於EK1 EK01 EK001。即EK0001缺失。 – Zeeshan2307

回答

0

更改getPaddedValue(字符串toTest)

在每次循環的時候,你的paddedvalue附加一個新值,長度(),您需要檢查是

EK01

EK001

EK0001

EK1 EK01 EK001 EK0001

出於這個原因,paddedvalue的長度將大於6在第二循環,然後斷裂。這會讓你錯誤的結果。

你需要讓每個子字符串,並檢查長度小於6

private static final String getPaddedValue(String toTest) { 
    StringBuilder paddedvalue = new StringBuilder(); 
    int zeroCounter = countOfAvailableZeroInToken(toTest); 
    for (int i = 0; i < 3 - zeroCounter; i++) { 
     paddedvalue.append(toTest.substring(0, 2)); 
     for(int j= 0; j<=i; j++){ 
      paddedvalue.append("0"); 
     } 
     paddedvalue.append(toTest.split("[a-zA-Z]{2}")[1]).append(" "); 

     //change your if condition like this 
     String lastPaddedValue = paddedvalue.toString().split("\\s")[paddedvalue.toString().split("\\s").length - 1]; 
     if(lastPaddedValue.length()-1>=6){ 
      break; 
     } 
    } 
    return paddedvalue.toString(); 
} 
+0

請您詳細說明您的答案添加一點關於您提供的解決方案的更多描述? – abarisone

+0

非常感謝哈斯。它工作正常:) – Zeeshan2307

0

更改getPaddedValue(String ...)

輸出:

[EK001] >> [EK001 EK0001 ] 
[MUS EK874] >> [MUS EK874 EK0874 ] 
[EK08] >> [EK08 EK008 EK0008 ] 
[MUS EK1] >> [MUS EK1 EK01 EK001 EK0001 ] 
[MUS EK47] >> [MUS EK47 EK047 EK0047 ] 
[EK0001] >> [EK0001 ] 
[E asfddsfsf EF549 dsgffdgfdgfdg] >> [E asfddsfsf EF549 EF0549 dsgffdgfdgfdg ] 
[AB09] >> [AB09 AB009 AB0009 ] 
[EK7 765kj 34h] >> [EK7 EK07 EK007 EK0007 765kj 34h ] 
[EK1 AB2] >> [EK1 EK01 EK001 EK0001 AB2 AB02 AB002 AB0002 ] 
+0

感謝噸Gosu ....工作正常:) – Zeeshan2307